vk

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2021 License: BSD-3-Clause Imports: 18 Imported by: 3

README

Api client for VKontakte with login/pass authorization (hack) on Go (golang).

###Plus: masking client_id to the iPhone, Android, iPad, Windows Phone clients.

go (golang) api client for vk.com

###Get

    go get github.com/yanple/vk_api
    // and dependence
    go get github.com/PuerkitoBio/goquery

###Import

    @import "github.com/yanple/vk_api"

##How to use

###Login/pass auth

	var api vk_api.Api
	err := api.LoginAuth(
		"email/phone",
		"pass",
		"3087104", // client id
		"wall,offline", // scope (permissions)
	)
	if err != nil {
		panic(err)
	}

###By user auth (click "allow" on special vk page)

	var api vk_api.Api
	authUrl, err := api.GetAuthUrl(
		"domain.com/method_get_access_token", // redirect URI
		"token", // response type
		"4672050", // client id
		"wall,offline", // permissions https://vk.com/dev/permissions
	)
	if err != nil {
		panic(err)
	}
	YourRedirectFunc(authUrl)

	//	And receive token on the special method (redirect uri)
	currentUrl := getCurrentUrl() // for example "yoursite.com/get_access_token#access_token=3304fdb7c3b69ace6b055c6cba34e5e2f0229f7ac2ee4ef46dc9f0b241143bac993e6ced9a3fbc111111&expires_in=0&user_id=1"
	accessToken, userId, expiresIn, err := vk_api.ParseResponseUrl(currentUrl)
	if err != nil {
		panic(err)
	}
	api.AccessToken = accessToken
	api.UserId = userId
	api.ExpiresIn = expiresIn

###Make query to API

	params := make(map[string]string)
	params["domain"] = "yanple"
	params["count"] = "1"

	strResp := api.Request("wall.get", params)
	if strResp != "" {
		pretty.Println(strResp)
	}

All api methods on https://vk.com/dev/methods

###Client ids (Masking only for login/pass auth)

    // client_id = "28909846" # Vk application ID (Android) doesn't work.
	// client_id = "3502561"  # Vk application ID (Windows Phone)
	// client_id = "3087106"  # Vk application ID (iPhone)
	// client_id = "3682744"  # Vk application ID (iPad)
License

Vk_api by Yanple is BSD licensed

Documentation

Index

Constants

View Source
const (
	METHOD_ACCOUNT_GET_BANNED  = "account.getBanned"
	METHOD_ACCOUNT_SET_ONLINE  = "account.setOnline"
	METHOD_ACCOUNT_SET_OFFLINE = "account.setOffline"

	METHOD_AUDIO_GET           = "audio.get"
	METHOD_AUDIO_SET_BROADCAST = "audio.setBroadcast"

	METHOD_AUTH_SIGNUP  = "auth.signup"
	METHOD_AUTH_CONFIRM = "auth.confirm"

	METHOD_BOARD_GET_COMMENTS = "board.getComments"

	METHOD_GROUPS_GET         = groupsPrefix + "get"
	METHOD_GROUPS_GET_BY_ID   = groupsPrefix + "getById"
	METHOD_GROUPS_GET_MEMBERS = groupsPrefix + "getMembers"
	METHOD_GROUPS_JOIN        = groupsPrefix + "join"
	METHOD_GROUPS_LEAVE       = groupsPrefix + "leave"
	METHOD_GROUPS_SEARCH      = groupsPrefix + "search"
	METHOD_GROUPS_IS_MEMBER   = groupsPrefix + "isMember"

	METHOD_MESSAGES_SEND          = "messages.send"
	METHOD_MESSAGES_GET           = "messages.get"
	METHOD_MESSAGES_GET_DIALOGS   = "messages.getDialogs"
	METHOD_MESSAGES_MARK_AS_READ  = "messages.markAsRead"
	METHOD_MESSAGES_GET_HISTORY   = "messages.getHistory"
	METHOD_MESSAGES_SET_ACTIVITY  = "messages.setActivity"
	MethodMessageFromGroupAllowed = "messages.isMessagesFromGroupAllowed"

	METHOD_LIKES_ADD      = "likes.add"
	METHOD_LIKES_DELETE   = "likes.delete"
	METHOD_LIKES_IS_LIKED = "likes.isLiked"
	METHOD_LIKES_GET_LIST = "likes.getList"

	METHOD_WALL_EDIT           = "wall.edit"
	METHOD_WALL_GET            = "wall.get"
	METHOD_WALL_GET_BY_ID      = "wall.getById"
	METHOD_WALL_POST           = "wall.post"
	METHOD_WALL_REPOST         = "wall.repost"
	METHOD_WALL_DELETE         = "wall.delete"
	METHOD_WALL_GET_REPOSTS    = "wall.getReposts"
	METHOD_WALL_CREATE_COMMENT = "wall.createComment"
	METHOD_WALL_GET_COMMENTS   = "wall.getComments"

	METHOD_FRIENDS_GET             = "friends.get"
	METHOD_FRIENDS_GET_REQUESTS    = "friends.getRequests"
	METHOD_FRIENDS_ARE_FRIENDS     = "friends.areFriends"
	METHOD_FRIENDS_ADD             = "friends.add"
	METHOD_FRIENDS_DELETE          = "friends.delete"
	METHOD_FRIENDS_GET_MUTUAL      = "friends.getMutual"
	METHOD_FRIENDS_GET_SUGGESTIONS = "friends.getSuggestions"

	METHOD_PHOTOS_GET                    = "photos.get"
	METHOD_PHOTOS_GET_ALL                = "photos.getAll"
	METHOD_PHOTOS_GET_WALL_UPLOAD_SERVER = "photos.getWallUploadServer"
	METHOD_PHOTOS_SAVE_WALL_PHOTO        = "photos.saveWallPhoto"

	MethodPoolsGetByID   = "polls.getById"
	MethodPoolsGetVoters = "polls.getVoters"

	METHOD_USERS_GET               = "users.get"
	METHOD_USERS_GET_FOLLOWERS     = "users.getFollowers"
	METHOD_USERS_SEARCH            = "users.search"
	METHOD_USERS_GET_SUBSCRIPTIONS = "users.getSubscriptions"

	METHOD_STATUS_SET = "status.set"

	METHOD_DATABASE_GET_CITIES       = "database.getCities"
	METHOD_DATABASE_GET_CITIES_BY_ID = "database.getCitiesById"

	METHOD_UTILS_RESOLVE_SCREEN_NAME = "utils.resolveScreenName"

	METHOD_NEWSFEED_ADD_BAN = "newsfeed.addBan"

	METHOD_EXECUTE = "execute"
)

Variables

View Source
var (
	APIURL         = "https://api.vk.com/method/"
	OAuthURL       = "https://oauth.vk.com"
	AuthHost       = OAuthURL + "/authorize"
	AccessTokenURL = OAuthURL + "/access_token"

	DefaultRedirectURI = "https://oauth.vk.com/blank.html"

	DefaultUserAgent = `VKAndroidApp/4.8.3-1113 (Android 4.4.2; SDK 10; armeabi-v7a; ZTE ZTE Blade L3; ru)`

	// VkAPIVersion is lastest vk.com api version supported
	VkAPIVersion = "5.131"
)
View Source
var (
	ErrCaptchaNeeded = fmt.Errorf("Captcha needed")
)
View Source
var (
	// RequestFreq is request limiter
	RequestFreq = 333 * time.Millisecond
)

Functions

func GetAccessTokenByCode

func GetAccessTokenByCode(code string, clientID string, clientSecret string, redirectURI string) (accessToken string, userID int64, err error)

GetAccessTokenByCode return's access_token with authcode flow https://vk.com/dev/authcode_flow_user

func GetAuthURL

func GetAuthURL(redirectURI string, responseType string, clientID string, scope string) (string, error)

GetAuthURL return url to auth in VK

func ParseResponseUrl

func ParseResponseUrl(responseUrl string) (string, string, string, error)

func Sig

func Sig(param url.Values, method, secret string) string

Types

type Api

type Api struct {
	AccessToken string
	UserId      string
	ExpiresIn   string

	PhoneCode string

	ClientId     int
	ClientSecret string

	StdCaptcha bool

	Lang  string
	Https bool

	LastCall time.Time

	CaptchaResolver CaptchaResolver
	// contains filtered or unexported fields
}

Api main struct

func NewApi

func NewApi(at string) *Api

func (*Api) CacheDir

func (vk *Api) CacheDir(s string)

func (*Api) HTTPClient

func (a *Api) HTTPClient() *http.Client

func (*Api) LoginAuth

func (vk *Api) LoginAuth(email string, password string, client_id string, scope string) error

func (*Api) Request

func (vk *Api) Request(methodName string, p ...url.Values) ([]byte, error)

func (*Api) RequestContext

func (vk *Api) RequestContext(ctx context.Context, method string, p ...url.Values) ([]byte, error)

func (*Api) RequestTypedContext

func (vk *Api) RequestTypedContext(ctx context.Context, method string, params url.Values, result interface{}) error

func (*Api) SetDebug

func (vk *Api) SetDebug(s bool)

func (*Api) SetHTTPClient

func (a *Api) SetHTTPClient(client *http.Client)

type CaptchaResolver

type CaptchaResolver interface {
	ResolveCaptcha(ctx context.Context, ssid string, imageURL string) (string, error)
}

type ErrResponse

type ErrResponse struct {
	Error struct {
		Code        int    `json:"error_code"`
		Msg         string `json:"error_msg"`
		CaptchaSid  string `json:"captcha_sid"`
		CapthchaImg string `json:"captcha_img"`
	} `json:"error"`
}

type Value

type Value struct {
	Key   string
	Value string
}

type Values

type Values []Value

func NewValuesFromParam

func NewValuesFromParam(param url.Values) Values

func (Values) Len

func (v Values) Len() int

func (Values) Less

func (v Values) Less(i, j int) bool

func (Values) Sig

func (v Values) Sig(method, secret string) string

func (Values) Swap

func (v Values) Swap(i, j int)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL