Google API Client “refresh token must be passed in or set as part of setAccessToken”

后端 未结 9 1380
Happy的楠姐
Happy的楠姐 2020-12-05 07:55

I am currently facing a very strange problem, indeed I\'ve been following this very same guide (https://developers.google.com/google-apps/calendar/quickstart/php) from Googl

9条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-05 08:35

    You need to serialize the accestoken when you write it to the credentialsPath.

     // Exchange authorization code for an access token.
        $accessToken = $client->authenticate($authCode);
    
        // Store the credentials to disk.
        if(!file_exists(dirname($credentialsPath))) {
            mkdir(dirname($credentialsPath), 0700, true);
        }
        $serArray = serialize($accessToken);
        file_put_contents($credentialsPath, $serArray);
        printf("Credentials saved to %s\n", $credentialsPath);
    

    And when you read from the file you need to unserialize it.

    if (file_exists($credentialsPath)) {
        $unserArray =  file_get_contents($credentialsPath);
        $accessToken = unserialize($unserArray);
    
    }
    

    Full function

    function getClient() {
        $client = new Google_Client();
        // Set to name/location of your client_secrets.json file.
        $client->setAuthConfigFile('client_secret.json');
        // Set to valid redirect URI for your project.
        $client->setRedirectUri('http://localhost');
        $client->setApprovalPrompt('force');
    
        $client->addScope(Google_Service_YouTube::YOUTUBE_READONLY);
        $client->setAccessType('offline');
    
        // Load previously authorized credentials from a file.
        $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    
    
        if (file_exists($credentialsPath)) {
            $unserArray =  file_get_contents($credentialsPath);
            $accessToken = unserialize($unserArray);
    
        } 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->authenticate($authCode);
    
            // Store the credentials to disk.
            if(!file_exists(dirname($credentialsPath))) {
                mkdir(dirname($credentialsPath), 0700, true);
            }
            $serArray = serialize($accessToken);
            file_put_contents($credentialsPath, $serArray);
            printf("Credentials saved to %s\n", $credentialsPath);
        }
    
        $client->setAccessToken($accessToken);
    
        // Refresh the token if it's expired.
        if ($client->isAccessTokenExpired()) {
            $client->refreshToken($client->getRefreshToken());
            file_put_contents($credentialsPath, $client->getAccessToken());
        }
        return $client;
    }
    

提交回复
热议问题