get Google client using access token

≡放荡痞女 提交于 2019-12-10 21:28:35

问题


I already stored the access and refresh token in my database. I want get the google client using that. I don't know how to use it in below example

$client = Zend_Gdata_ClientLogin::getHttpClient('you@there.com', 'password', Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME);
$service = new Zend_Gdata_Spreadsheets($client);

// Get worksheet feed
$query = new Zend_Gdata_Spreadsheets_DocumentQuery();
$query->setSpreadsheetKey('your spreadsheet key');
$feed = $spreadsheetService->getWorksheetFeed($query);

I want replace email and password with access token. Someone help me how to do that. I tried below. but I got only exception

Caught exception: Expected response code 200, got 401

and Stateless token expired

$client = Zend_Gdata_AuthSub::getHttpClient('ya29.XXXXXXX'); 

Another try,

$client = new Zend_Gdata_HttpClient();
$session_token =Zend_Gdata_AuthSub::getAuthSubSessionToken('ya29.XXXXXXX',$client);
$client->setAuthSubToken($sessionToken);

Caught exception: Token upgrade failed. Reason:


回答1:


I think you are mixing things up.

ClientLogin and AuthSub are different authentication APIs (both deprecated). The ClientLogin token expires after two weeks or earlier (see: https://developers.google.com/gdata/faq#clientlogin_expire). You can use the token as long as it does not expire by calling the setter setClientLoginToken of Zend_Gdata_HttpClient.

Example:

$client = Zend_Gdata_ClientLogin::getHttpClient('email@example.com', 'password', Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME);
$token = $client->getClientLoginToken();
//Save to DB / in session / whatever
$client = new Zend_Gdata_HttpClient();
$client->setClientLoginToken($token);
//Do stuff

You can also cache the whole HttpClient object with Zend_Cache.

AuthSub or OAuth 2.0 (better, but no classes provided by ZF) is maybe better for your needs, because the tokens do not expire (AuthSub) or can be refreshed (OAuth2)

Docs

AuthSub:

  • ZF Docs > http://framework.zend.com/manual/1.12/de/zend.gdata.authsub.html
  • Revoke unlimited token > https://developers.google.com/accounts/docs/AuthSub?hl=de#AuthSubRevokeToken

OAuth2:

  • Refresh tokens > https://developers.google.com/accounts/docs/OAuth2WebServer?hl=de#refresh


来源:https://stackoverflow.com/questions/18013564/get-google-client-using-access-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!