我正在做一个项目,让客户授权他们的谷歌广告帐户,然后使用这些授权凭据,以他们的名义下载数据。我有一个webapp,成功地授权应用程序为客户做事情。这将生成一个访问代码,然后我交换两个凭据,一个访问令牌和一个刷新令牌。然后将此刷新令牌传递到数据库,在数据库中,一个单独的应用程序尝试查询googleAds API。据我了解,Google引擎只需要刷新令牌。
我试图通过使用load_from_dict()或load_from_env()方法对GoogleAdsClient类进行授权。两者产生相同的错误:google.auth.exceptions.RefreshError: ('invalid_client: Unauthorized', {'error': 'invalid_client', 'error_description': 'Unauthorized'})
我已经验证了developer_token、client_id和client_secret对于API控制台中的内容都是准确的。我还验证了refresh_token正被正确地传递给证书dict。
我真的不知所措,不知从哪里出发。我读过许多具有类似标题的堆栈溢出线程,但我仍然停留在相同的位置。
这里有一些相关的链接。
相关代码
class GoogleAds:
def __init__(self):
self.scope = ['https://www.googleapis.com/auth/adwords']
self.client_id = os.getenv('GOOGLE_ADS_CLIENT_ID')
self.client_secret = os.getenv('GOOGLE_ADS_CLIENT_SECRET')
self.developer_token = os.getenv('GOOGLE_ADS_DEVELOPER_TOKEN')
self.refresh_token = os.getenv('GOOGLE_ADS_REFRESH_TOKEN')
def authorize(self):
credentials = {
"developer_token": self.developer_token,
"refresh_token": self.refresh_token,
"client_id": self.client_id,
"client_secret": self.client_secret,
"use_proto_plus":"True",
"grant_type": "refresh_token",
}
print(credentials)
googleads_client = GoogleAdsClient.load_from_dict(credentials)
service = googleads_client.get_service("GoogleAdsService")
request = googleads_client.get_type("SearchGoogleAdsRequest")
return service, request
发布于 2022-10-24 11:47:17
'error': 'invalid_client', 'error_description': 'Unauthorized'
可能是一个很难调试的错误。它可能意味着很多事情。
目前,这意味着用户还没有授权这个客户端。
首先,确保刷新令牌未过期。其次,确保用于创建刷新令牌的客户端id和客户端机密文件与请求新访问令牌的客户端id和客户端保密文件相同。
https://stackoverflow.com/questions/74175745
复制相似问题