供应APi redirect_uri

时间:2014-11-02 17:42:28

标签: google-analytics google-analytics-api

我正在使用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授权服务器发出信号   授权代码应作为查询字符串参数返回   客户端上的Web服务器

所以这个redirect_uri应该有效。

根据规定api docs

  

重定向URI - 这是用户重定向到的位置和OAuth   发送2.0响应。使用Google Developers Console配置重定向URI并获取项目的客户端ID。的价值   此参数必须与在其中注册的值之一完全匹配   Google Developers Console(包括http或https计划,案例,   和尾随')。

如何指定配置API的重定向URI以及localhost无效的原因?

1 个答案:

答案 0 :(得分:1)

由于我还不能发表评论,我会留下这个答案。

  

body_ = {'redirectUri':'http // localhost',

一目了然,

http//localhost should be http://localhost