Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

php - Google API refreshToken() giving error "Unauthorized client"

This following code works fine but it expires after 1 hour

$client->setAccessToken($accessToken);  //$accessToken contains access_token, refresh_token etc

But instead of above line if I do

$client->refreshToken('1/c1f43fvAbSSPHTQ8GipjNop6SutKUTvHGOZwDcWop2I'); //dummy code

It gives this error:

Google_AuthException: Error refreshing the OAuth2 token, message: '{

"error" : "unauthorized_client" }' in Google_OAuth2->refreshTokenRequest() (line 288 of ..google-api-php-clientsrcauthGoogle_OAuth2.php).

What could be the problem?

Also in the approval URL approval_prompt=force is set.

The actual URL is

https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&client_id=974066854442545-k8ib4o9f7rneb4b1jkeah072ln27at39ss.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.readonly&access_type=offline&approval_prompt=force

Complete Code

(I've obfuscated the codes here) =>Actual calling is done in function medicalentitlement_cron

<?php

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once "google-api-php-client/src/contrib/Google_Oauth2Service.php";

// ...

$CLIENT_ID = '1111111-45342534524353245345345.apps.googleusercontent.com';
$CLIENT_SECRET = '111111111111111111111';
$REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob';
$SCOPES = array(
    'https://www.googleapis.com/auth/drive.file',
    'https://www.googleapis.com/auth/userinfo.email',
    'https://www.googleapis.com/auth/userinfo.profile');

/**
 * Exception thrown when an error occurred while retrieving credentials.
 */
class GetCredentialsException extends Exception
    {

    protected
            $authorizationUrl;

    /**
     * Construct a GetCredentialsException.
     *
     * @param authorizationUrl The authorization URL to redirect the user to.
     */
    public
            function __construct($authorizationUrl)
    {
        $this->authorizationUrl = $authorizationUrl;
    }

    /**
     * @return the authorizationUrl.
     */
    public
            function getAuthorizationUrl()
    {
        return $this->authorizationUrl;
    }

    /**
     * Set the authorization URL.
     */
    public
            function setAuthorizationurl($authorizationUrl)
    {
        $this->authorizationUrl = $authorizationUrl;
    }

    }

/**
 * Exception thrown when no refresh token has been found.
 */
class NoRefreshTokenException extends GetCredentialsException
    {

    }

/**
 * Exception thrown when a code exchange has failed.
 */
class CodeExchangeException extends GetCredentialsException
    {

    }

/**
 * Exception thrown when no user ID could be retrieved.
 */
class NoUserIdException extends Exception
    {

    }

/**
 * Retrieved stored credentials for the provided user ID.
 *
 * @param String $userId User's ID.
 * @return String Json representation of the OAuth 2.0 credentials.
 */
function getStoredCredentials($userId)
{
    $arr = unserialize(file_get_contents(drupal_get_path('module', medicalentitlement) . "/googleapps.sr"));
    print "Returning arr1= $arr[1]";
    return $arr[1];
}

/**
 * Store OAuth 2.0 credentials in the application's database.
 *
 * @param String $userId User's ID.
 * @param String $credentials Json representation of the OAuth 2.0 credentials to
  store.
 */
function storeCredentials($userId, $credentials)
{
    // TODO: Implement this function to work with your database.
    print "userId=$userId  and credentials=$credentials
";

    $arr = array();
    $arr[] = $userId;
    $arr[] = $credentials;

    file_put_contents("googleapps.sr", serialize($arr));
    return;
}

/**
 * Exchange an authorization code for OAuth 2.0 credentials.
 *
 * @param String $authorizationCode Authorization code to exchange for OAuth 2.0
 *                                  credentials.
 * @return String Json representation of the OAuth 2.0 credentials.
 * @throws CodeExchangeException An error occurred.
 */
function exchangeCode($authorizationCode)
{
    try
        {
        global $CLIENT_ID, $CLIENT_SECRET, $REDIRECT_URI;
        $client = new Google_Client();

        $client->setClientId($CLIENT_ID);
        $client->setClientSecret($CLIENT_SECRET);
        $client->setRedirectUri($REDIRECT_URI);
        $_GET['code'] = $authorizationCode;
        return $client->authenticate();
        }
    catch (Google_AuthException $e)
        {
        print 'An error occurred: ' . $e->getMessage();
        throw new CodeExchangeException(null);
        }
}

/**
 * Send a request to the UserInfo API to retrieve the user's information.
 *
 * @param String credentials OAuth 2.0 credentials to authorize the request.
 * @return Userinfo User's information.
 * @throws NoUserIdException An error occurred.
 */
function getUserInfo($credentials)
{
    $apiClient = new Google_Client();
    $apiClient->setUseObjects(true);
    $apiClient->setAccessToken($credentials);
    $userInfoService = new Google_Oauth2Service($apiClient);
    $userInfo = null;
    try
        {
        $userInfo = $userInfoService->userinfo->get();
        }
    catch (Google_Exception $e)
        {
        print 'An error occurred: ' . $e->getMessage();
        }
    if ($userInfo != null && $userInfo->getId() != null)
        {
        return $userInfo;
        }
    else
        {
        throw new NoUserIdException();
        }
}

/**
 * Retrieve the authorization URL.
 *
 * @param String $emailAddress User's e-mail address.
 * @param String $state State for the authorization URL.
 * @return String Authorization URL to redirect the user to.
 */
function getAuthorizationUrl($emailAddress, $state)
{
    global $CLIENT_ID, $REDIRECT_URI, $SCOPES;
    $client = new Google_Client();

    $client->setClientId($CLIENT_ID);
    $client->setRedirectUri($REDIRECT_URI);
    $client->setAccessType('offline');
    $client->setApprovalPrompt('force');
    $client->setState($state);
    $client->setScopes($SCOPES);
    $tmpUrl = parse_url($client->createAuthUrl());
    $query = explode('&', $tmpUrl['query']);
    $query[] = 'user_id=' . urlencode($emailAddress);
    return
            $tmpUrl['scheme'] . '://' . $tmpUrl['host'] . $tmpUrl['port'] .
            $tmpUrl['path'] . '?' . implode('&', $query);
}

/**
 * Retrieve credentials using the provided authorization code.
 *
 * This function exchanges the authorization code for an access token and
 * queries the UserInfo API to retrieve the user's e-mail address. If a
 * refresh token has been retrieved along with an access token, it is stored
 * in the application database using the user's e-mail address as key. If no
 * refresh token has been retrieved, the function checks in the application
 * database for one and returns it if found or throws a NoRefreshTokenException
 * with the authorization URL to redirect the user to.
 *
 * @param String authorizationCode Authorization code to use to retrieve an access
 *                                 token.
 * @param String state State to set to the authorization URL in case of error.
 * @return String Json representation of the OAuth 2.0 credentials.
 * @throws NoRefreshTokenException No refresh token could be retrieved from
 *         the available sources.
 */
function getCredentials($authorizationCode, $state)
{
    $emailAddress = '';
    try
        { {

            $credentials = getStoredCredentials($userId);
            $credentialsArray = json_decode($credentials, true);

            print "credential = $credentials
";
            if ($credentials != null &&
                    isset($credentialsArray['refresh_token']))
                {
                return $credentials;
                }
        }
        }
    catch (CodeExchangeException $e)
        {
        print __LINE__."
";
        print 'An error occurred during code exchange.';
        // Drive apps should try to retrieve the user and credentials for the current
        // session.
        // If none is available, redirect the user to the authorization URL.
        $e->setAuthorizationUrl(getAuthorizationUrl($emailAddress, $state));
        throw $e;
        }
    catch (NoUserIdException $e)
        {
        print 'No e-mail address could be retrieved.';
        }
    // No refresh token has been retrieved.
    $authorizationUrl = getAuthorizationUrl($emailAddress, $state);
    throw new NoRefreshTokenException($authorizationUrl);
}

function medicalentitlement_cron()
{

    $client = new Google_Client();
// Get your credentials from the console
    $client->setClientId($CLIENT_ID);
    $client->setClientSecret($CLIENT_SECRET);
    $client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
    $client->setScopes(array('https://www.googleapis.com/auth/drive', 'email'));

    $service = new Google_DriveService($client);


    //IF I UNCOMMENT THESE TWO  AND COMMENT THE THIRD THEN IT WORKS
    //  $accessToken = getCredentials($authCode, 'anu');
    //  $client->setAccessToken($accessToken);

        $client->refreshToken('1/qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq');

    try
        {

        $fileid = '0B4534545425456545643564356';
        $file = $service->files->get($fileid);


        print "file=" . gettype($file) . "
";
        var_dump($file);
        $downloadUrl = $file['downloadUrl'];

        //$downloadUrl = $file->getDownloadUrl();

        if ($downloadUrl)
            {
            $request = new Google_HttpRequest($downloadUrl, 'GET', null, null);
            $httpRequest = Google_Client::$io->authenticatedRequest($request);
            if ($httpRequest->getResponseHttpCode() == 200)
                {
                file_put_contents("d:/tmp/bb.xlsx", $httpRequest->getResponseBody());
                }
            else
                {
                // An error occurred.
                return null;
                }
            }
        else
            {
            // The file doesn't have any content stored on Drive.
            return null;
            }
        }
    catch (Exception $e)
        {
        print "An error occurred: " . $e->getMessage();
        }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...