我正在尝试设置谷歌分析到我的一个博客网站。 我使用的是google api客户端库。 如果我从网站上手动获取访问令牌,代码工作正常。https:/developers.google.comoauthplayground。. 我发现很难有访问_token刷新。 有人能指导我吗,因为我很新的谷歌分析。 将分享的python代码片段,请。
import httplib2
import httplib2 as lib2
import google.oauth2.credentials
# from google.auth.transport import requests
import requests
from google_auth_httplib2 import AuthorizedHttp
from oauth2client import client, GOOGLE_TOKEN_URI
from googleapiclient.discovery import build as google_build
# This is consistent for all Google services
token_uri = 'https://accounts.google.com/o/oauth2/token'
# WORKING CODE BUT REQUIRES VALID ACCESS_TOKEN
credentials = google.oauth2.credentials.Credentials(None,
refresh_token=refresh_token,
token_uri='https://accounts.google.com/o/oauth2/token',
client_id=client_id,
client_secret=client_secret)
credentials = google.oauth2.credentials.Credentials(access_token)
authorized = AuthorizedHttp(credentials=credentials)
print(access_token)
# API Name and Verison, these don't change until
# they release a new API version for us to play with.
api_name = 'analyticsreporting'
api_version = 'v4'
# Let's build the client
api_client = google_build(serviceName=api_name, version=api_version, http=authorized)
sample_request = {
'viewId': 'xxxxxxxx',
'dateRanges': {
'startDate': datetime.strftime(datetime.now() - timedelta(days=120), '%Y-%m-%d'),
'endDate': datetime.strftime(datetime.now(), '%Y-%m-%d')
},
'dimensions': [{'name': 'ga:date'}],
'metrics': [{'expression': 'ga:sessions'}]
}
response = api_client.reports().batchGet(
body={
'reportRequests': sample_request
}).execute()
print(response)
我一直保持access_token为none来获取初始访问令牌,这似乎是工作,当我打印它,但我不能得到一个方法来刷新这个access_token,使用这个凭证api,因为这没有expires_at或到期属性以及。URL范围将是'https:/www.googleapis.comauthanalytics.readonly'
当运行上面的代码来刷新ACCESS TOKEN时,得到了错误。
Traceback (most recent call last):
File "E:/PyCharm_Workspace/MLPractices/GA3.py", line 78, in <module>
'reportRequests': sample_request
File "E:\PyCharm_Workspace\MLPractices\venv\lib\site-packages\googleapiclient\_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "E:\PyCharm_Workspace\MLPractices\venv\lib\site-packages\googleapiclient\http.py", line 901, in execute
headers=self.headers,
File "E:\PyCharm_Workspace\MLPractices\venv\lib\site-packages\googleapiclient\http.py", line 177, in _retry_request
resp, content = http.request(uri, method, *args, **kwargs)
File "E:\PyCharm_Workspace\MLPractices\venv\lib\site-packages\google_auth_httplib2.py", line 213, in request
self.credentials.refresh(self._request)
File "E:\PyCharm_Workspace\MLPractices\venv\lib\site-packages\google\oauth2\credentials.py", line 172, in refresh
"The credentials do not contain the necessary fields need to "
google.auth.exceptions.RefreshError: The credentials do not contain the necessary fields need to refresh the access token. You must specify refresh_token, token_uri, client_id, and client_secret.
谢谢
Yuva
你会得到刷新token与访问token的第一次。你的刷新令牌是长期存在的。所以你可以将它存储在数据库中,以便以后使用。每当你需要一个访问令牌时,使用该刷新令牌并生成一个新的令牌。
Google API:使用oauth2client.client从刷新令牌中获取凭证。
上面的链接会对你有所帮助。
谢谢大家。 可以通过使用服务账号的凭证来获得google analytics的访问权限,而不是OAUTH - client_secrets。 使用服务账户似乎很简单,因为现在我不需要刷新任何令牌。
但我在想哪个更好,更安全,任何想法,请。 此外,这是最新的参考访问谷歌分析V4,在2020年发布的参考。https:/www.jcchouinard.comgoogle-analytics-api-using-python