我无法使用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)我是否需要在console.developers.google.com中为每位AdWords客户设置一个特殊项目,以便从AdWords Reporting API中获取?或者,我可以在控制台中简单地为通用帐户提供客户端密钥和ID吗?
2)在此之后,有人可以确认应该代替client_ID& Client_Secret,以使Python代码块在下面工作。我的意思是,我使用https://console.developers.google.com中的客户端ID和客户端密码...用于我们开具账单设置的分析账户(以及之前用于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客户端提取一份报告(一旦我到达那里,我会扩展)。任何帮助,将不胜感激。欢呼声。
答案 0 :(得分:8)
经过一番工作,我能够成功地解决这个问题。以下是我可以通过API成功提取数据的详细步骤。在我的情况下,我管理了一个包含多个帐户的AdWords MCC。因此,我回到了许多帮助手册的开头并做了以下事情:
在此之后,我创建了一个新的googleads.yaml脚本并将其放在我的c:\ gsutil目录中。这是程序在大多数Python程序中查找文件googleads.yaml:
的代码adwords_client = adwords.AdWordsClient.LoadFromStorage()
完成此操作后,我能够从命令行成功运行脚本以生成最终输出。脚本是:
python download_criteria_report.py
当然注意我之前已经更改了我的路径变量,以便从命令行运行Python 2.7。此脚本在download_criteria_report.py文件的目录中运行。此脚本成功运行,使我能够从AdWords API中为我的某个测试客户端提取数据。
接下来的挑战将是使用API返回的输出并将其放入我可以快速用于分析的格式中。存储