且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

请求 GA API 时为 400(错误请求)

更新时间:2022-06-19 07:03:42

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

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
}

希望这会有所帮助.