ArcGIS Onlineの一部のデータにアクセスするためにトークンを必要とするHTMLファイルがあります。別のJavaScriptファイルでサービスを呼び出し、トークンを取得する必要があります。トークンはHTMLファイルに何らかの形で渡す必要があります。これは私が不確かなビットです。いずれの場合においてもJavaScriptレスポンスをHTMLに変換する方法
、コード:
のJavaScriptファイル(GetAToken.js)
var request = require('request'); // npm install request
// generate a token with your client id and client secret
function getToken(callback)
{
request.post(
{
url: 'https://www.arcgis.com/sharing/rest/oauth2/token/',
json: true,
form:
{
'f': 'json',
'client_id': '<<MY CLIENT_ID>>',
'client_secret': '<<MY CLIENT_SECRET>>',
'grant_type': 'client_credentials',
'expiration': '1440'
}
}, function (error, response, body)
{
console.log(body.access_token);
callback(body.access_token);
});
}
そしてHTML
<script src="GetAToken.js"></script>
</head>
<body onload="getToken()">
<div class="embed-container">
<iframe width="500"
height="400"
frameborder="0"
scrolling="no"
marginheight="0"
marginwidth="0"
title="Test Map" src="//MyMap.maps.arcgis.com/apps/webappviewer/index.html?id=LongMapID?token=I_Need_My_Token_Here&extent=1,-1,1,-1&zoom=true&scale=true&search=true&searchextent=true&legend=true&basemap_gallery=true&disable_scroll=true&theme=light">
</iframe>
</div>
</body>
</html>
からの関連ビット、あなたは内で見ればHTMLのdiv、それは私のトークンが必要な場所です。 JavaScriptが明らかにaccess_token
と呼ばれる値を返し、使用してNode.jsの
EDIT
新GetAToken.js
const request = require('request'); // npm install request
const express = require('express');
const app = express();
// generate a token with your client id and client secret
//function getToken(callback)
app.get('/GetAToken', (req, res) => {
request.post(
{
url: 'https://www.arcgis.com/sharing/rest/oauth2/token/',
json: true,
form:
{
'f': 'json',
'client_id': '<<MY_CLIENT_ID>>',
'client_secret': '<<MY_CLIENT_SECRET>>',
'grant_type': 'client_credentials',
'expiration': '1440'
}
}, function (error, response, body) {
console.log(body.access_token);
callback(body.access_token);
});
});
app.listen(80);
あなたがしている
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Title</title>
<link href="https://esri.github.io/calcite-bootstrap/assets/css/calcite-bootstrap-open.min.css" rel="stylesheet">
<style>
.footer
{
height: 6.25rem;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://esri.github.io/calcite-bootstrap/assets/js/bootstrap.min.js"></script>
<script src="https://js.arcgis.com/3.17/"></script>
<script src="GetAToken.js"></script>
<script type="text/javascript">
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if(xhttp.readyState === 4 && xhttp.status === 200)
{
var responseJSON = JSON.parse(xhttp.responseText);
var token = responseJSON.token;
alert(token);
}
}
xhttp.open("GET", "GetAToken", true);
xhttp.send();
</script>
</head>
<body>
<style>
.embed-container
{
position: relative;
padding-bottom: 50%;
height: 0;
max-width: 100%;
}
.embed-container iframe, .embed-container object, .embed-container iframe
{
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
small
{
position: absolute;
z-index: 40;
bottom: 0;
margin-bottom: -15px;
}
</style>
<div class="embed-container">
<iframe width="500"
height="400"
frameborder="0"
scrolling="no"
marginheight="0"
marginwidth="0"
title="Test Map" src="//MyMap.maps.arcgis.com/apps/webappviewer/index.html?id=MyLongID&extent=1,-1,1,-1&zoom=true&scale=true&search=true&searchextent=true&legend=true&basemap_gallery=true&disable_scroll=true&theme=light">
</iframe>
</div>
</body>
</html>
代わりに、それをクライアントに提供する前にHTMLにトークンを挿入することにより、これを解決することができ、サーバーまたはクライアント上で実行されている 'GetAToken'ですか? –
わかりません。私はJavaScriptを初めて使っています。それは現時点で私のHTMLと同じフォルダにあり、私はそれをインターネット上で最終的に実行したいと思うでしょう。 – user25730
秘密鍵があるため、サーバー上で実行する必要があります。 – alexi2