JSON始终返回" client_id未指定"

时间:2016-07-25 06:05:39

标签: javascript json arcgis

我尝试使用Javascript从ArcGIS Online服务获取令牌。但是,它始终返回一个错误,表明client_id未被指定。

我在这里做的一切吗?

<script type="text/javascript">
    var MyJSONText = '{"client_id":"<<MY_CLIENT_ID>>","client_secret":"<<MY_CLIENT_SECRET>>","grant_type":"client_credentials","expiration":"1440","f":"json"}';
    var MyJSON = JSON.parse(MyJSONText);
    xhr = new XMLHttpRequest();
    xhr.open("POST", "https://www.arcgis.com/sharing/rest/oauth2/token/");
    xhr.send(MyJSON);
    xhr.onreadystatechange = function ()
    {
        if (xhr.readyState == 4 && xhr.status == 200)
        {
            alert(xhr.responseText);
        }
    }

</script>

编辑 - 完整错误是:

{"error":{"code":400,"error":"invalid_request","error_description":"client_id not specified","message":"client_id not specified","details":[]}}

2 个答案:

答案 0 :(得分:1)

我能够使用if body, ok := message.Body.(*icmp.Echo); ok { // do something with Body as an *icmp.Echo type } 请求检索访问令牌:

application/x-www-form-urlencoded

这意味着您在制作XHR请求时可能需要指定Content-Type请求标头:

POST https://www.arcgis.com/sharing/rest/oauth2/token HTTP/1.1
User-Agent: Fiddler
Content-Type: application/x-www-form-urlencoded
Host: www.arcgis.com
Content-Length: 126

client_id=<YOUR ID>&client_secret=<YOUR SECRET>&grant_type=client_credentials&expiration=1440&f=json

当然正确地将正文格式化为xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 而不是JSON。在我的测试中,此端点不适用于JSON有效负载。

不幸的是,从它的外观来看,令牌端点不支持在其CORS策略中设置application/x-www-form-urlencoded请求标头,这意味着使用javascript调用它可能会运气不佳。除their documentation之外,Content-Type没有提及javascript作为受支持的语言。

所以基本上如果你想做这项工作,你可以在服务器端获得访问令牌并将其传递给客户端。

答案 1 :(得分:0)

访问令牌生成网址=> https://www.arcgis.com/sharing/rest/oauth2/token

标题 enter image description here

对于“邮递员中的尸体”,请选择“ x-www-form-urlencoded”

enter image description here

之后,您将获得带有令牌的相应预期结果

{"access_token":"S-z3mxAqsZBeihx8NgFmNGsZAUGfzKZWCYVV2TiQO422u9XDLTCTZAkpoecyxx_LAc71I_tPkeeOlo2Pzkapv01bBOA03SzMRjRMOm-h3ljTl-pb3XdffoyYjCflE4F8LD377DbpjDWkupkdTaxJpg..","expires_in":7200}