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

后端 未结 9 1368
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:42

    I got the same problem recently and i solved it with this.

    setRedirectUri($this->_redirectURI);
     $client->setAccessType('offline');
     $client->setApprovalPrompt('force');
    

    I explain ..... Refresh token is not returned because we didnt force the approvalPrompt. The offline mode is not enought. We must force the approvalPrompt. Also the redirectURI must be set before these two options. It worked for me.

    This is my full function

    setApplicationName($this->projectName);
            $client->setScopes(SCOPES);
            $client->setAuthConfig($this->jsonKeyFilePath);
            $client->setRedirectUri($this->redirectUri);
            $client->setAccessType('offline');
            $client->setApprovalPrompt('force');
    
           // Load previously authorized credentials from a file.
           if (file_exists($this->tokenFile)) {
             $accessToken = json_decode(file_get_contents($this->tokenFile), 
             true);
          } else {
            // Request authorization from the user.
            $authUrl = $client->createAuthUrl();
            header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
    
            if (isset($_GET['code'])) {
                $authCode = $_GET['code'];
                // Exchange authorization code for an access token.
                $accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
                header('Location: ' . filter_var($this->redirectUri, 
                FILTER_SANITIZE_URL));
                if(!file_exists(dirname($this->tokenFile))) {
                    mkdir(dirname($this->tokenFile), 0700, true);
                }
    
                file_put_contents($this->tokenFile, json_encode($accessToken));
            }else{
                exit('No code found');
            }
        }
        $client->setAccessToken($accessToken);
    
        // Refresh the token if it's expired.
        if ($client->isAccessTokenExpired()) {
    
            // save refresh token to some variable
            $refreshTokenSaved = $client->getRefreshToken();
    
            // update access token
            $client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);
    
            // pass access token to some variable
            $accessTokenUpdated = $client->getAccessToken();
    
            // append refresh token
            $accessTokenUpdated['refresh_token'] = $refreshTokenSaved;
    
            //Set the new acces token
            $accessToken = $refreshTokenSaved;
            $client->setAccessToken($accessToken);
    
            // save to file
            file_put_contents($this->tokenFile, 
           json_encode($accessTokenUpdated));
        }
        return $client;
    }
    

提交回复
热议问题