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

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

问题描述

尝试访问Google Auth时,我得到了 400 Bad Request 。我在本文中使用演示代码:
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&安培;即时=真安培; include_granted_scopes =真安培;代理= oauth2relay452650337&安培; REDIRECT_URI = PostMessage的&安培;来源= HTTP%3A%2F%2Flocalhost%3A8080&安培; RESPONSE_TYPE =令牌安培;状态= 895891795%7C0.1215960807&安培; AUTHUSER = 0



我成功创建了自己的ClientId和Keys。



需要照顾吗?



谢谢, 有解决方案

你的请求有几个错误。首先是范围,其次是respons_type。我不确定您发现该示例的网页上的哪个位置。你应该真的尝试找到一个图书馆,以确定你正在使用哪种语言,这会使它更加容易。但是,如果你想知道你应该发布的确切的URL,他们应该看起来像这样。






请求用户给你访问帐户应该看起来像这样在这种情况下,范围通知我的范围是不同的,那么你的,我正在请求一个代码:

  https://accounts.google.com/o/oauth2/auth?client_id= {的clientid} .apps.googleusercontent.com&安培; REDIRECT_URI =瓮:IETF:WG:OAuth的:2.0:OOB&安培; scope = https://www.googleapis.com/auth/analytics.readonly& response_type = code 

一旦他们说出是的话,你就可以从上面的请求中获得认证代码,并将其发送回请求一个access_token和一个refresh_token

  https://accounts.google.com/o/oauth2/token 
代码= 4 / X9lG6uWd8-MMJPElWggHZRzyFKtp.QubAT_P-GEwePvB8fYmgkJzntDnaiAI&安培; CLIENT_ID = {客户端Id} .apps.googleusercontent.com&安培; client_secret = {ClientSecret}&安培; REDIRECT_URI =瓮:IETF:瓦特g:oauth:2.0:oob& grant_type = authorization_code

这是回应:



'pre> {
的access_token: ya29.1.AADtN_VSBMC2Ga2lhxsTKjVQ_ROco8VbD6h01aj4PcKHLm6qvHbNtn-_BIzXMw,
的token_type: 承载,
的expires_in:3600,
的refresh_token: 1 / J-3zPA8XR1o_cXebV9sDKn_f5MTqaFhKFxH-3PUPiJ4
}

您从上述请求中获得的accesstoken是您将用于向服务发出请求的用途。一小时之后,您的访问令牌已经过期,您需要申请一个新的访问令牌,然后将您获得的refreshtoken发送至:

  https://accounts.google.com/o/oauth2/token 
的client_id = {客户端Id} .apps.googleusercontent.com&安培; client_secret = {ClientSecret}&安培; refresh_token = 1 / ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&安培; grant_type = refresh_token

这是回应:

<$
access_token:ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ,
token_type:持票人,
expires_in:3600

希望这会有所帮助。

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

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

I have successfully created my own ClientId and Keys.

Is there anything I need to take care of?

Thanks,

解决方案

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.


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

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

this is the response:

{
  "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

This is the response:

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

Hope this helps.

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

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