Can’t execute method groups.get
. VK API
returns:
[error_code] = & gt; 5 [error_msg] = & gt; User authorization failed: no
access_token passed
However, other methods (getCities
, getCountries
) work.
This line does not work correctly:
$ groups = json_decode (@file_get_contents ('https://api.vk.com/method/groups.get ? uid '. $ token [user_id].' '));
Maybe the problem is that when receiving token [expires_in] = & gt; 0
?
Here’s the code:
& lt ;?
require_once ('common_use.php');
session_start ();
$ client_id = '***';
$ client_secret = '****';
$ redirect_uri = 'http: //site/outh.php';
$ result = false;
if (isset ($ _ GET ['code'])) {
$ params = array (
'client_id' = & gt; $ client_id,
'client_secret' = & gt; $ client_secret,
'code' = & gt; $ _GET ['code'],
'redirect_uri' = & gt; $ redirect_uri
);
$ token = json_decode (@file_get_contents ('https://oauth.vk.com/access_token'. '?'. urldecode (http_build_query ($ params))), true);
if (isset ($ token ['access_token'])) {
$ params = array (
'uids' = & gt; $ token ['user_id'],
'fields' = & gt; 'uid, city, country, first_name, last_name, screen_name, sex, bdate, photo_big, photo_rec, contacts',
'access_token' = & gt; $ token ['access_token']
);
$ userInfo = json_decode (@file_get_contents ('https://api.vk.com/method/users.get?'. urldecode (http_build_query ($ params))), true);
if (isset ($ userInfo ['response'] [0] ['uid'])) {
$ userInfo = $ userInfo ['response'] [0];
$ result = true;
// Here is an error
$ groups = json_decode (@file_get_contents ('https://api.vk.com/method/groups.get?uid'.$token [user_id].' '));
$ sig = md5 ("api_id =". $ client_id. "method = getCitiesv = 2.0api_secret =". $ client_secret. "");
$ res = json_decode (@file_get_contents ('https://api.vk.com/method/getCities?api_id='.$client_id.'&sig='.$sig.'&v=2.0&cids= '. $ userInfo [city].' '));
$ city = $ res - & gt; response [0] - & gt; name;
}
}
}
? & gt;
Answer 1, authority 100%
So, since you are using the so-called two-step method to obtain a token, the following snippet will help you. You will need the client_id and client_secret of your application. After allowing access, the snippet gets the groups of the logged in user, easy to change.
& lt;? php
$ client_id = xxxxxxx;
$ client_secret = "xxxxxxxx";
? & gt;
& lt; a href = "https://oauth.vk.com/authorize?client_id=<?=$client_id;?>&redirect_uri=http://vm-0.roxy.kd.io/& response_type = code & amp; v = 5.0 "& gt; Login & lt; / a & gt;
& lt;? php
if (! empty ($ _ GET ["code"])) {
$ code = $ _GET ["code"];
$ query = file_get_contents ("https://api.vk.com/oauth/access_token?client_id= {$client_id }&client_secret= {$client_secret>&code= {$code}");
$ res = json_decode ($ query, 1);
$ token = $ res ["access_token"];
echo "& lt; h1 & gt; ur token is: & lt; / h1 & gt;". $ token;
// getting groups;
$ query = file_get_contents ("https://api.vk.com/method/groups.get?user_id= {$ res [" user_id "]} & amp; extended = 1 & amp; access_token = {$ token}");
echo "& lt; pre & gt;";
print_r (json_decode ($ query, 1));
echo "& lt; / pre & gt;";
}
? & gt;