以下代码可以正常运行,但会在1小时后过期
$client->setAccessToken($accessToken); //$accessToken contains access_token, refresh_token etc
但如果我这样做,而不是上线
$client->refreshToken('1/c1f43fvAbSSPHTQ8GipjNop6SutKUTvHGOZwDcWop2I'); //dummy code
它给出了这个错误:
Google_AuthException:刷新OAuth2令牌时出错,消息:'{
“错误”:“unauthorized_client”}' Google_OAuth2-> refreshTokenRequest()(第288行) .. \谷歌-API-PHP-客户\ SRC \ AUTH \ Google_OAuth2.php)。
可能是什么问题?
同样在批准网址中,设置了approval_prompt = force。
实际网址为
(我在这里对代码进行了模糊处理) =>实际调用在函数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\n";
$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\n";
if ($credentials != null &&
isset($credentialsArray['refresh_token']))
{
return $credentials;
}
}
}
catch (CodeExchangeException $e)
{
print __LINE__."\n";
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) . "\n";
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();
}
}