bje*_*lli 16 python google-api google-plus
我正在使用googles官方oauth2client.client访问google plus api.我有一个存储在数据库中的刷新令牌(不会过期),需要从中重新创建临时"凭据"(访问令牌).
但我无法找到一种方法来与谷歌提供的官方图书馆这样做.
所以我讨厌它:使用urllib访问API,从refresh_token给我一个新的access_token.使用access_token我可以使用该库.
我一定是想念一下!
from apiclient import discovery
from oauth2client.client import AccessTokenCredentials
from urllib import urlencode
from urllib2 import Request , urlopen, HTTPError
import json
# ==========================================
def access_token_from_refresh_token(client_id, client_secret, refresh_token):
request = Request('https://accounts.google.com/o/oauth2/token',
data=urlencode({
'grant_type': 'refresh_token',
'client_id': client_id,
'client_secret': client_secret,
'refresh_token': refresh_token
}),
headers={
'Content-Type': 'application/x-www-form-urlencoded',
'Accept': 'application/json'
}
)
response = json.load(urlopen(request))
return response['access_token']
# ==========================================
access_token = access_token_from_refresh_token(CLIENT_ID, CLIENT_SECRET, REFRESH_TOKEN)
# now I can use the library properly
credentials = AccessTokenCredentials(access_token, "MyAgent/1.0", None)
http = credentials.authorize(httplib2.Http())
service = discovery.build('plus', 'v1', http=http)
google_request = service.people().get(userId='me')
result = google_request.execute(http=http)
Run Code Online (Sandbox Code Playgroud)
小智 19
我使用:oauth2client.client.GoogleCredentials
cred = oauth2client.client.GoogleCredentials(access_token,client_id,client_secret,
refresh_token,expires_at,"https://accounts.google.com/o/oauth2/token",some_user_agent)
http = cred.authorize(httplib2.Http())
cred.refresh(http)
self.gmail_service = discovery.build('gmail', 'v1', credentials=cred)
Run Code Online (Sandbox Code Playgroud)
你可以OAuth2Credentials像这样直接构造一个实例:
import httplib2
from oauth2client import GOOGLE_REVOKE_URI, GOOGLE_TOKEN_URI, client
CLIENT_ID = '<client_id>'
CLIENT_SECRET = '<client_secret>'
REFRESH_TOKEN = '<refresh_token>'
credentials = client.OAuth2Credentials(
access_token=None, # set access_token to None since we use a refresh token
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET,
refresh_token=REFRESH_TOKEN,
token_expiry=None,
token_uri=GOOGLE_TOKEN_URI,
user_agent=None,
revoke_uri=GOOGLE_REVOKE_URI)
credentials.refresh(httplib2.Http()) # refresh the access token (optional)
print(credentials.to_json())
http = credentials.authorize(httplib2.Http()) # apply the credentials
Run Code Online (Sandbox Code Playgroud)
我很容易解决这个问题(你肯定会错过这个文档).这是我的代码片段,尝试使用Picasa API从活动用户获取所有相册:
http = httplib2.Http(ca_certs=os.environ['REQUESTS_CA_BUNDLE'])
try:
http = self.oauth.credentials.authorize(http)
response, album_list = http.request(Picasa.PHOTOS_URL, 'GET')
if response['status'] == '403':
self.oauth.credentials.refresh(http)
response, album_list = http.request(Picasa.PHOTOS_URL, 'GET')
album_list = json.load(StringIO(album_list))
except Exception as ex:
Logger.debug('Picasa: error %s' % ex)
return {}
Run Code Online (Sandbox Code Playgroud)
使用refresh来自oauth2client.client.OAuth2Credentials的方法.我认为它甚至可以使用if response['status'] != '200'.得检查一下!
您可以存储整个凭据,而不仅仅是刷新令牌:
json = credentials.to_json()
credentials = Credentials.new_from_json(json)
Run Code Online (Sandbox Code Playgroud)
查看以这种方式执行此操作的Storage 对象。
| 归档时间: |
|
| 查看次数: |
20018 次 |
| 最近记录: |