请求 GA API 时为 400(错误请求) [英] 400 (Bad Request) when requesting GA API

查看:25
本文介绍了请求 GA API 时为 400(错误请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试访问 Google 身份验证时收到 400 Bad Request.我正在使用本文中的演示代码:https://developers.google.com/analytics/solutions/articles/hello-analytics-api

I got a 400 Bad Request when trying to access Google Auth. I am using the demo code in this article: https://developers.google.com/analytics/solutions/articles/hello-analytics-api

我请求的地址是:https://accounts.google.com/o/oauth2/auth?client_id=875938******.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly&immediate=true&include_granted_scopes=true&proxy=oauth2relay452650337&redirect_uri=postmessage&origin=http%3A%2F%2Flocalhost%3A8080&response_type1.89&705000000000000000000&response_type=token;response_type1.89&705050000代码>

The address I am requesting is: https://accounts.google.com/o/oauth2/auth?client_id=875938******.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fanalytics.readonly&immediate=true&include_granted_scopes=true&proxy=oauth2relay452650337&redirect_uri=postmessage&origin=http%3A%2F%2Flocalhost%3A8080&response_type=token&state=895891795%7C0.1215960807&authuser=0

我已经成功创建了自己的 ClientId 和 Keys.

I have successfully created my own ClientId and Keys.

有什么需要我照顾的吗?

Is there anything I need to take care of?

谢谢,

推荐答案

您的请求有几个问题.首先是范围,其次是响应类型.我不确定您在链接页面上的哪个位置找到了该示例.您真的应该尝试为您使用的任何语言找到一个库,这将使它更容易.但是,如果您想知道应该发布的确切网址,它们应该看起来像这样.

There are several things wrong with your request. First the scope, second the respons_type. I'm not sure where on the page you linked that you found that example. You should really try and find a library for what ever language you are using it will make it easer. But if you want to know the exact URLs you should be posting they should look something like this.

请求用户授予您访问该帐户的权限的初始 URI 应如下所示在这种情况下,范围通知我的范围与您的范围不同,我正在请求代码:

The initial URI to request that the user give you access to there account should look like this In this case the scope notice my scope is different then yours and I'm requesting a code:

https://accounts.google.com/o/oauth2/auth?client_id={clientid}.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/analytics.readonly&response_type=code

一旦他们说是",您就可以获取从上述请求中获得的身份验证代码并将其发回以请求 access_token 和 refresh_token

Once they say yes you take the authentication Code you got from the above request and Post it back to request an access_token and a refresh_token

https://accounts.google.com/o/oauth2/token
code=4/X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code

这是回复:

{
  "access_token" : "ya29.1.AADtN_VSBMC2Ga2lhxsTKjVQ_ROco8VbD6h01aj4PcKHLm6qvHbNtn-_BIzXMw",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : "1/J-3zPA8XR1o_cXebV9sDKn_f5MTqaFhKFxH-3PUPiJ4"
}

您从上述请求中获得的访问令牌将用于向服务发出请求.一小时后,您的访问令牌将过期,您需要请求一个新的访问令牌,您将上面获得的刷新令牌发布到:

The accesstoken you get from the above request is what you will be using to make requests to the service. After one hour your access token will have expired you will need to request a new access token you take the refreshtoken that you got above and post it to :

https://accounts.google.com/o/oauth2/token
client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token

这是回复:

{
  "access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ",
  "token_type" : "Bearer",
  "expires_in" : 3600
}

希望这会有所帮助.

这篇关于请求 GA API 时为 400(错误请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆