Using a Service Account, getAccessToken() is returning null

前端 未结 1 1776
后悔当初
后悔当初 2020-12-18 04:12

I am running the following PHP code, using the client libraries found here: https://code.google.com/p/google-api-php-client/. I do not get any errors for any of this code,

相关标签:
1条回答
  • 2020-12-18 04:32

    For some reason, this seemed to work:

    require_once 'google-api-php-client/src/Google_Client.php';
    
    const CLIENT_ID = 'blahblahblah';
    const SERVICE_ACCOUNT_NAME = 'blahblahblah@developer.gserviceaccount.com';
    const KEY_FILE = 'path/to/privatekey.p12';
    const CALENDAR_SCOPE = "https://www.googleapis.com/auth/calendar";
    
    $key = file_get_contents(KEY_FILE);
    $auth = new Google_AssertionCredentials(
        SERVICE_ACCOUNT_NAME,
        array(CALENDAR_SCOPE),
        $key
    );
    
    $client = new Google_Client();
    $client->setScopes(array(CALENDAR_SCOPE));
    $client->setAssertionCredentials($auth);
    $client->getAuth()->refreshTokenWithAssertion();
    $accessToken = $client->getAccessToken();
    
    $client->setClientId(CLIENT_ID);
    

    If someone can explain why this worked, please edit this answer or comment!

    0 讨论(0)
提交回复
热议问题