Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Google Api获取访问权\u令牌请求返回无效\u请求_Javascript_Jquery_Xmlhttprequest_Google Api_Oauth 2.0 - Fatal编程技术网

Javascript Google Api获取访问权\u令牌请求返回无效\u请求

Javascript Google Api获取访问权\u令牌请求返回无效\u请求,javascript,jquery,xmlhttprequest,google-api,oauth-2.0,Javascript,Jquery,Xmlhttprequest,Google Api,Oauth 2.0,我正在尝试使用javascript获取Google APi的access\u令牌,并且总是收到错误消息:无效的\u请求。这是我的代码: var options = { url: tokenURL, type: "POST", headers: { "Content-type": "application/x-www-form-urlencoded"}, dataType: "json", data: { "code":successCode

我正在尝试使用javascript获取Google APi的access\u令牌,并且总是收到错误消息:
无效的\u请求
。这是我的代码:

var options = {
    url: tokenURL,
    type: "POST",
    headers: { "Content-type": "application/x-www-form-urlencoded"},
    dataType: "json",
    data: {
        "code":successCode,
        "client_id": clietId,
        "client_secret": clientSecret,
        "grant_type": "authorization_code",
        "redirect_url": "urn:ietf:wg:oauth:2.0:oob"
    },
    complete: function (e) {
        alert(e.status);
    },
};

$.ajax(options);
我还试着用简单的html表单发出POST请求,结果很好

<form method="post" action="https://accounts.google.com/o/oauth2/token">
<input name="code" type="text" value="##code##" />
<input name="client_id" type="text" value="##client_id##" />
<input name="client_secret" type="text" value="##client_secret##" />
<input name="grant_type" type="text" value="authorization_code" />
<input name="redirect_uri" type="text" value="urn:ietf:wg:oauth:2.0:oob" />

<input type="submit" /></form>


我不知道javascript请求有什么问题。是否缺少一些参数或标题?

看起来
数据的编码(在第一个示例中)与内容类型不匹配

数据的编码似乎是application/json,但指定的内容类型是application/x-www-form-urlencoded

   data: "code=successCode&client_id=clientId&client_secret=clientSecret&grant_type=authorization_code&redirect_url=urn:ietf:wg:oauth:2.0:oob"
您需要将
数据
的编码更改为URL编码

   data: "code=successCode&client_id=clientId&client_secret=clientSecret&grant_type=authorization_code&redirect_url=urn:ietf:wg:oauth:2.0:oob"

尝试使用Firebug并查看Net选项卡,以查看您从表单发出的请求与ajax请求之间的差异。我尝试使用Fiddler,但没有注意到任何差异,除了cookies@pauliusnrk您是否得到了解决方案?通过像您所说的那样设置数据,甚至不会发送请求:
var options={url:'https://accounts.google.com/o/oauth2/token,类型:“POST”,标题:{“内容类型”:“application/x-www-form-urlencoded”},数据类型:“json”,数据:“code=code&client\u id=clientId&clientSecret=clientSecret&redirect\u uri=myuri&grant\u type=authorization\u code”,完成:函数(e){alert(e);alert(e.status);},};$.ajax(选项);