我在使用Python为AdWords API生成刷新令牌时遇到问题,需要一些帮助。情况如下:
下面是生成我正在尝试使用的刷新令牌所需的以下代码段:
"""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) 我需要有一个特别的项目设置为每个AdWords的客户在控制台.developers.google.com,以便从AdWords报告API中提取?或者,我可以简单地在控制台中提供通用帐户的客户端密码和ID吗?在
2)接下来,有人可以确认应该用什么代替client_ID和client_Secret,以使下面的Python代码块正常工作。我的意思是,我使用了来自https://console.developers.google.com的客户机ID和客户机密码。。。对于我们设置了计费的分析帐户(我以前曾使用它访问过BigQuery API)。对吗?我不清楚这将如何链接到这个客户的AdWords帐户。在
2)在同意屏幕中,我放了我自己的电子邮件,因为我是项目的所有者,。也就是说,当我运行代码时,我得到了生成代码所需运行的URL的链接。也就是说,当我晒出这段片段时:
^{pr2}$我得到一个错误。这是我收到的错误消息:
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获取数据。在我的情况下,我管理一个有多个帐户的AdWords MCC。因此,我回到许多帮助手册的开头,并执行了以下操作:
在这之后,我创建了一个新的鹅眼草.yaml编写脚本并将其放入我的c:\gsutil目录中。这是大多数Python程序中查找文件的代码鹅眼草.yaml公司名称:
完成后,我就可以从命令行成功地运行脚本来生成最终的输出。剧本是:
当然请注意,为了从命令行运行Python2.7,我之前更改了path变量。此脚本在下载条件的目录中运行_报表.py文件。这个脚本成功运行,使我能够从AdWords API中为我的一个测试客户机提取数据。在
下一个挑战是使用API返回的输出,并将其转换为可以快速用于分析和存储的格式。在
相关问题 更多 >
编程相关推荐