2014-02-26 295 views
0

尝试访问Google身份验证时,我收到了400 Bad Request。我使用本文中的演示代码: https://developers.google.com/analytics/solutions/articles/hello-analytics-api请求GA API时400(错误请求)

我请求的地址是: 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

我已经成功地创建了自己的客户端Id和密钥。

有什么我需要照顾的?

谢谢,

回答

1

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


初始URI请求用户让你访问那里帐户应该是这样的。在这种情况下,范围注意到我的范围不同,那么你的,我请求代码:

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认证码和后

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

您从上述请求中获得的accesstoken是您将用于向服务发出请求的用途。一小时后您的访问令牌将已过期,您将需要请求你把你上面得到了refreshtoken新的访问令牌,并邮寄到:

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 
} 

希望这可以帮助。

+0

谢谢,我没有改变任何原始演示代码,除了替换我自己的ClientId和Key。有时它有时不起作用。这很混乱。 –

+0

你使用什么语言? – DaImTo

+0

我正在使用JavaScript,我尝试了浏览器密钥和服务器密钥。两者都是一样的。 –