小编Le *_*oom的帖子

带有PHP服务帐户的Google OAuth 2.0:"无效的令牌格式错误"

我创建了我的服务帐户,获得了private_key并委托了域范围的权限.

这是我的代码尝试使用服务帐户进行身份验证,但获得相同的"无效的令牌格式错误":

session_start();
include_once 'google-api-php/vendor/autoload.php';

function getClient() {
$client = new Google_Client();
$client->setApplicationName('theName');
$client->setScopes('https://www.googleapis.com/auth/admin.directory.user.readonly');
$client->setAccessType('offline');
$client->setSubject('admin@domain.com');
$client->setAuthConfig('private_key.json');

// Load previously authorized credentials from a file.
$credentialsPath = 'private_key.json';
if (file_exists($credentialsPath)) {
    $accessToken = json_decode(file_get_contents($credentialsPath), true);
}
else {
    // Request authorization from the user.
    $authUrl = $client->createAuthUrl();
    printf("Open the following link in your browser:\n%s\n", $authUrl);
    print 'Enter verification code: ';
    $authCode = trim(fgets(STDIN));

    // Exchange authorization code for an access token.
    $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);

    // Store the credentials to disk. …
Run Code Online (Sandbox Code Playgroud)

php oauth-2.0 google-api-php-client service-accounts google-admin-sdk

1
推荐指数
1
解决办法
5448
查看次数