javascript
  • json
  • arcgis
  • 2016-07-25 36 views 1 likes 
    1

    我试图从ArcGIS Online服务使用Javascript获取令牌。但是,它总是返回一个错误,指示未指定client_id。JSON始终返回“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":[]}} 
    

    回答

    1

    我能够检索使用application/x-www-form-urlencoded请求访问令牌:

    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请求时输入请求标题:

    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
    

    ,当然正确格式化为application/x-www-form-urlencoded而不是JSON。在我的测试中,这个端点不支持JSON有效载荷。

    不幸的是,从它的外观来看,令牌端点不支持在其CORS策略中设置Content-Type请求标头,这意味着您在使用javascript调用它时可能会运气不好。除了their documentation没有提及任何有关javascript作为支持的语言。

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

    +0

    在console.log中的预检响应中,带回了'请求标头字段'Content-type不被允许通过Access-Control-Allow-Headers。 – user25730

    +0

    对不起,仍然回来'client_id未指定'错误。 – user25730

    +0

    是的,仍然有同样的错误。无论如何感谢 - 将它标记为答案。看起来我必须重新评估我使用的方法,并更多地了解如何使用node.js – user25730

    相关问题