Answer the question
In order to leave comments, you need to log in
How to get a standalone application token from the site?
Good day.
Are there any solutions that can be used to get a user's token from standalone authorization on the site?
According to the documentation , the required permissions will only be obtained if the redirect_uri is specified as blank.html .
In particular, I need access to the photos.delete
method.
I authorize on my site in a new window. Trying to catch location.href from the parent window of course throws an error
Blocked a frame with origin " mysqte " from accessing a cross-origin frame
Answer the question
In order to leave comments, you need to log in
you can create a token that is not limited in time, incl. if the application is tied to a specific account, then you simply generate it in the browser and enter the received token on the
OAuth2Vk.php website
use VK\OAuth\VKOAuth;
use VK\OAuth\VKOAuthResponseType;
use VK\OAuth\VKOAuthDisplay;
use VK\OAuth\Scopes\VKOAuthUserScope;
use VK\Client\VKApiClient;
class OAuth2Vk
{
protected $configPath;
protected $config;
public function __construct($configPath)
{
$this->configPath = $configPath;
$this->config = include($configPath);
if (empty($this->config['state'])) {
$this->config['state'] = str_shuffle('abcdefghijklmnopqrstuvwxyz1234567890');
}
}
public function setAccessToken(array $data)
{
if ($data['state'] != $this->config['state']) {
throw new InvalidArgumentException('Invalid request state');
}
$this->config['access_token'] = $data['access_token'];
$this->config['expires_in'] = time() + $data['expires_in'];
$this->config['user_id'] = $data['user_id'];
$this->config['state'] = null;
}
public function getAuthorizeUrl($revoke_auth = false)
{
return (new VKOAuth)->getAuthorizeUrl(
VKOAuthResponseType::TOKEN, // VKOAuthResponseType::CODE
$this->config['client_id'],
$this->config['redirect_uri'],
VKOAuthDisplay::PAGE, // VKOAuthDisplay::POPUP
[VKOAuthUserScope::OFFLINE, VKOAuthUserScope::WALL, VKOAuthUserScope::GROUPS],
$this->config['state'],
null,
true
);
}
public function hasAccessTokenExpired()
{
// $expires_in = $this->config['expires_in'] ?: 0;
return empty($this->config['expires_in']) || $this->config['expires_in'] <= time();
}
public function addPost(array $post)
{
if ($this->hasAccessTokenExpired()) {
throw new InvalidArgumentException('Access token expired');
}
return (new VKApiClient)->wall()->post(
$this->config['access_token'],
[
'owner_id' => $this->config['user_id'],
'message' => $post['message'],
'attachments' => [$post['link']],
'guid' => time(),
'close_comments' => 0,
'mute_notifications' => 0,
]
);
}
public function __destruct()
{
file_put_contents(
$this->configPath,
'<?php return ' . var_export($this->config, true) . ';'
);
}
}
<?php
return [
'client_id' => 7...9,
'client_secret' => '...',
'group_id' => 1...9,
'redirect_uri' => '.../oauth2/redirect/vk/',
'base_uri' => '.../oauth2/base/vk/',
'state' => '123',
];
$oauth = new OAuth2Vk(__DIR__ . '/oauth2/vk.php');
if (filter_input(INPUT_GET, 'access_token')) {
$oauth->setAccessToken($_GET);
header('Location: vk.php');
} elseif ($oauth->hasAccessTokenExpired()) {
header('Location: ' . $oauth->getAuthorizeUrl());
} else {
try {
$response = $oauth->addPost([
'message' => 'Проверка связи! https://site.ru the end!',
'link' => 'https://site.ru/test-link/',
]);
var_dump($response);
} catch (Exception $e) {
var_dump($e);
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question