简体   繁体   English

JSON 总是返回“client_id 未指定”

[英]JSON always returning “client_id not specified”

I'm trying to get a token from the ArcGIS Online service using a Javascript.我正在尝试使用 Javascript 从 ArcGIS Online 服务获取令牌。 However, it's always returning an error which indicates the client_id isn't specified.但是,它总是返回一个错误,表明未指定 client_id。

Am I doing everything right here?我在这里做所有事情吗?

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

Edit - full error is:编辑 - 完整错误是:

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

I was able to retrieve an access token using application/x-www-form-urlencoded request:我能够使用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

which means that you might need to specify the Content-Type request header when making the XHR request:这意味着您可能需要在发出 XHR 请求时指定 Content-Type 请求标头:

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

and of course properly formatting the body as application/x-www-form-urlencoded instead of JSON.当然,将正文正确格式化为application/x-www-form-urlencoded而不是 JSON。 In my tests this endpoint didn't work with JSON payload.在我的测试中,此端点不适用于 JSON 有效负载。

Unfortunately from what it looks, the token endpoint doesn't support setting the Content-Type request header in its CORS policy which means that you might be out of luck in calling it with javascript.不幸的是,从它的外观来看,令牌端点不支持在其 CORS 策略中设置Content-Type请求标头,这意味着您可能无法使用 javascript 调用它。 Besides their documentation doesn't mention anything about javascript as a supported language.除了their documentation没有提到任何关于javascript作为受支持语言的内容。

So basically if you want to make this work you could get the access token on your server side and pass it along to the client.所以基本上,如果你想完成这项工作,你可以在服务器端获取访问令牌并将其传递给客户端。

Access Token Generation Url => https://www.arcgis.com/sharing/rest/oauth2/token访问令牌生成 URL => https://www.arcgis.com/sharing/rest/oauth2/token

Headers enter image description here标题在此处输入图像描述

For the Case of body in Post man select 'x-www-form-urlencoded'对于 Post man 中的 body 案例,选择“x-www-form-urlencoded”

enter image description here在此处输入图片说明

After that you will get the respective expected result with token之后,您将使用令牌获得相应的预期结果

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM