【发布时间】:2014-11-16 00:48:53
【问题描述】:
我正在使用https://developers.google.com/analytics/devguides/config/provisioning/v3/devguide
对于身份验证,我使用“已安装的应用程序方法”,并从开发者控制台获得了以下 json。
{
"installed":{
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"client_secret":"MYSECRET",
"token_uri":"https://accounts.google.com/o/oauth2/token",
"client_email":"",
"redirect_uris":["urn:ietf:wg:oauth:2.0:oob","oob"],
"client_x509_cert_url":"",
"client_id":"MY CLIENT ID",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs"
} }
根据文档,我必须执行以下操作:
import httplib2
from apiclient.discovery import build
from oauth2client.file import Storage
def create_account_ticket(service):
"""for the body see
https://developers.google.com/analytics/devguides/config/provisioning/v3/devguide
search for Create an Account Ticket using the Provisioning API
and take a look at:
https://developers.google.com/resources/api
libraries/documentation/analytics/v3/python/latest/analytics_v3.provisioning.html
"""
body_ = {'redirectUri': 'http://localhost',
'account': {'name': "My Account Name"},
'webproperty': {'name': 'What kind of name?',
'websiteUrl': 'http://www.mywebsite.de'},
'profile': {'name': 'My Profile name', 'timezone': "Europe/Berlin"},
}
res = service.provisioning().createAccountTicket(body=body_).execute()
return res
if __name__ == '__main__':
storage = Storage('FileContainingToken.dat')
credentials = storage.get()
http = httplib2.Http()
http = credentials.authorize(http)
service = build('analytics', 'v3', http=http)
t = create_account_ticket(service)
当我将 redirect_uri 设置为 localhost 时,响应是:
HttpError:https://www.googleapis.com/analytics/v3/provisioning/createAccountTicket?alt=json 返回“字段值 redirectUrl = `http://localhost` 无效。">
当我删除 redirect_uri 时,我得到了预期: “需要字段重定向 URI”
根据https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi
本地主机
此值向 Google 授权服务器发出信号,即 授权码应作为查询字符串参数返回 客户端上的网络服务器
所以这个redirect_uri应该是有效的。
根据规定api docs
重定向 URI - 这是用户被重定向到的位置和 OAuth 发送 2.0 响应。使用 Google Developers Console 配置重定向 URI 并获取项目的客户端 ID。的价值 此参数必须完全匹配在 Google Developers Console(包括 http 或 https 方案、案例、 和尾随'/')。
如何为配置 API 指定重定向 URI 以及为什么 localhost 无效?
【问题讨论】:
标签: google-analytics google-analytics-api