我正在使用 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
localhost
This value signals to the Google Authorization Server that the authorization code should be returned as a query string parameter to the web server on the client
所以这个redirect_uri 应该是有效的。
根据规定 api docs
Redirect URI - This is where the user is redirected to and the OAuth 2.0 response is sent. Configure Redirect URIs and obtain the Client ID for your project using the Google Developers Console. The value of this parameter must exactly match one of the values registered in the Google Developers Console (including the http or https schemes, case, and trailing '/').
如何为配置 API 指定重定向 URI 以及为什么 localhost 无效?
最佳答案
由于我还不能发表评论,我将把它作为这个答案。
body_ = {'redirectUri': 'http//localhost',
一眼望去,
http//localhost should be http://localhost
关于google-analytics - 配置 API redirect_uri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26702334/