Python 无法使用OAuth2为AdWords帐户生成刷新令牌
我在使用Python为AdWords API生成刷新令牌时遇到问题&需要一些帮助。情况如下:Python 无法使用OAuth2为AdWords帐户生成刷新令牌,python,google-oauth,google-ads-api,adwords-apiv201402,Python,Google Oauth,Google Ads Api,Adwords Apiv201402,我在使用Python为AdWords API生成刷新令牌时遇到问题&需要一些帮助。情况如下: 我在AdWords上有一个客户端,我想通过AdWords API获取报告(我们现在有一个开发者令牌)。假设,在AdWords中,客户帐户是521-314-0974(组成此帐户)。这就是我困惑的地方: 下面是生成我正在尝试使用的刷新令牌所需的以下代码段: """Generates a refresh token for use with AdWords.""" __author__ = 'Natha
- 我在AdWords上有一个客户端,我想通过AdWords API获取报告(我们现在有一个开发者令牌)。假设,在AdWords中,客户帐户是521-314-0974(组成此帐户)。这就是我困惑的地方:
"""Generates a refresh token for use with AdWords."""
__author__ = 'Nathaniel Payne'
import sys
import urllib2
from oauthlib import oauth2
# Your OAuth 2.0 Client ID and Secret. If you do not have an ID and Secret yet,
# please go to https://console.developers.google.com and create a set.
CLIENT_ID = 'INSERT_CLIENT_ID_HERE'
CLIENT_SECRET = 'INSERT_CLIENT_SECRET_HERE'
# You may optionally provide an HTTPS proxy.
HTTPS_PROXY = None
# The AdWords API OAuth 2.0 scope.
SCOPE = u'https://adwords.google.com/api/adwords'
# This callback URL will allow you to copy the token from the success screen.
CALLBACK_URL = 'urn:ietf:wg:oauth:2.0:oob'
# The HTTP headers needed on OAuth 2.0 refresh requests.
OAUTH2_REFRESH_HEADERS = {'content-type':
'application/x-www-form-urlencoded'}
# The web address for generating new OAuth 2.0 credentials at Google.
GOOGLE_OAUTH2_AUTH_ENDPOINT = 'https://accounts.google.com/o/oauth2/auth'
GOOGLE_OAUTH2_GEN_ENDPOINT = 'https://accounts.google.com/o/oauth2/token'
def main():
oauthlib_client = oauth2.WebApplicationClient(CLIENT_ID)
authorize_url = oauthlib_client.prepare_request_uri(
GOOGLE_OAUTH2_AUTH_ENDPOINT, redirect_uri=CALLBACK_URL, scope=SCOPE)
print ('Log in to your AdWords account and open the following URL: \n%s\n' %
authorize_url)
print 'After approving the token enter the verification code (if specified).'
code = raw_input('Code: ').strip()
post_body = oauthlib_client.prepare_request_body(
client_secret=CLIENT_SECRET, code=code, redirect_uri=CALLBACK_URL)
if sys.version_info[0] == 3:
post_body = bytes(post_body, 'utf8')
request = urllib2.Request(GOOGLE_OAUTH2_GEN_ENDPOINT, post_body,
OAUTH2_REFRESH_HEADERS)
if HTTPS_PROXY:
request.set_proxy(HTTPS_PROXY, 'https')
raw_response = urllib2.urlopen(request).read().decode()
oauth2_credentials = oauthlib_client.parse_request_body_response(raw_response)
print ('Your access token is %s and your refresh token is %s'
% (oauth2_credentials['access_token'],
oauth2_credentials['refresh_token']))
print ('You can cache these credentials into a yaml file with the '
'following keys:\nadwords:\n client_id: %s\n client_secret: %s\n'
' refresh_token: %s\n'
% (CLIENT_ID, CLIENT_SECRET, oauth2_credentials['refresh_token']))
if __name__ == '__main__':
main()
问题:
1) 我是否需要在console.developers.google.com中为每个AdWords客户设置一个特殊的项目,以便从AdWords Reporting API中提取数据?或者,我可以简单地为控制台中的通用帐户提供客户端密码和ID吗
2) 下面,请有人确认应该用什么来代替client\u ID和client\u Secret,以使下面的Python代码块工作。我的意思是,我使用的是来自。。。对于我们有账单设置的分析账户(我之前用于BigQuery API访问)。对吗?我不清楚这将如何与该客户的AdWords帐户联系起来
2) 在“同意”屏幕中,我放置了自己的电子邮件,因为我是项目的所有者,。也就是说,当我运行代码时,我会得到指向生成代码所需的URL的链接。也就是说,当我看到这段文字时:
print ('Log in to your AdWords account and open the following URL: \n%s\n' %
authorize_url)
print 'After approving the token enter the verification code (if specified).'
code = raw_input('Code: ').strip()
我犯了一个错误。这是我收到的错误消息:
Error: redirect_uri_mismatch
The redirect URI in the request: urn:ietf:wg:oauth:2.0:oob did not match a registered redirect URI
Learn more
Request Details
cookie_policy_enforce=false
scope=https://adwords.google.com/api/adwords
response_type=code
access_type=online
redirect_uri=urn:ietf:wg:oauth:2.0:oob
display=page
client_id=XXXXXXXXX.apps.googleusercontent.com
我在这里感到困惑。一些人建议更改同意屏幕中的电子邮件地址(我这样做了……但没有成功)。同样,我的简单目标是能够通过AdWords API从tis客户机中提取一份报告(一旦到达,我将扩展该API)。任何帮助都将不胜感激。干杯。经过一些工作,我成功地浏览了这个问题。下面是我为成功地通过API获取数据而采取的详细步骤。在我的情况下,我管理一个有多个帐户的AdWords MCC。因此,我回到许多帮助手册的开头,做了以下工作:
adwords_client = adwords.AdWordsClient.LoadFromStorage()
python download_criteria_report.py
下一个挑战是如何处理API返回的输出,并将其转换为我可以快速用于分析和存储的格式。好文章,对新手很有帮助,似乎缺少全面的指南!谢谢很高兴这有帮助。我也有同样的感觉。我正在慢慢地浏览Google云堆栈,并会在我前进的过程中发布问题(如果我找到了问题或其他人找到了问题,还会发布解决方案)。当我使用generate_refresh_token.py时,它给了我一个刷新令牌,这是开发者令牌吗?你的googleads.yaml文件是什么样子的?哦,nm,刚刚发现这个: