doudan1123 2016-09-30 13:36
浏览 24

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

I have created my service account, got the private_key and delegated domain wide authority.

Here is my code trying to authenticate with the service account but getting the same "Invalid token format error":

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:
%s
", $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.
    if(!file_exists(dirname($credentialsPath))) {
      mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, json_encode($accessToken));
    printf("Credentials saved to %s
", $credentialsPath);
}

$client->setAccessToken($accessToken);

// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
    $client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
    file_put_contents($credentialsPath, json_encode($client->getAccessToken()));
}
return $client;
}

And here is screenshot of what I get from $accessToken just before

$client->setAccessToken($accessToken);

with the error itself:

https://postimg.org/image/ajgan5y27/

Any help will be appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • duandan1995 2016-09-30 14:53
    关注

    The problem was the outdated google api documentation. Turns out the new version for the "getClient" function needs only this to work in case anyone is having troubles:

    function getClient() {
        $client = new Google_Client();
        $client->setAuthConfig('private_key.json');
        $client->setApplicationName('theName');
        $client->setScopes(Google_Service_Sheets::SPREADSHEETS_READONLY);
        return $client;
    }
    

    Doesn't need to $client->setAccessToken(); at all...

    Good job google... Those are the outdated and unreliable documentation pages I took this code from:

    https://developers.google.com/admin-sdk/directory/v1/quickstart/php and https://developers.google.com/api-client-library/php/auth/service-accounts

    One more thing: in case you need to play with Google Sheets, you may need to add the account service ID (xxxxxxxxxx@xxxxxxxxxxxxxx.iam.gserviceaccount.com) to the google sheet document you want to extract information from.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM