2016-07-24 9 views
0

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&amp;extent=1,-1,1,-1&amp;zoom=true&amp;scale=true&amp;search=true&amp;searchextent=true&amp;legend=true&amp;basemap_gallery=true&amp;disable_scroll=true&amp;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&amp;extent=1,-1,1,-1&amp;zoom=true&amp;scale=true&amp;search=true&amp;searchextent=true&amp;legend=true&amp;basemap_gallery=true&amp;disable_scroll=true&amp;theme=light"> 
     </iframe> 
    </div> 

</body> 
</html> 
+0

代わりに、それをクライアントに提供する前にHTMLにトークンを挿入することにより、これを解決することができ、サーバーまたはクライアント上で実行されている 'GetAToken'ですか? –

+0

わかりません。私はJavaScriptを初めて使っています。それは現時点で私のHTMLと同じフォルダにあり、私はそれをインターネット上で最終的に実行したいと思うでしょう。 – user25730

+0

秘密鍵があるため、サーバー上で実行する必要があります。 – alexi2

答えて

1

更新されたHTMLを書かれています何らかの形でクライアントに利用可能なarcgisへのリクエストの応答をしたいと思っています。ここで特急を使った例です:

const request = require('request'); // npm install request 
const express = require('express'); // npm install express 
const app = express(); 

app.get('/get-a-token', (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); 
       res.json({token: body.access_token}); 
     }); 
}); 

app.listen(80); 

次に、クライアント上で、サーバーから値を取得するには、このような何かを行うことができます:

<script type="text/javascript"> 
    // You may want to move this to another file.. 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
     if (xhttp.readyState === 4 && xhttp.status === 200) { 
      var responseJSON = JSON.parse(xhttp.responseText); 
      var token = responseJSON.token; 

      var iframe = document.querySelectorAll('iframe')[0] 
      iframe.src = "//MyMap.maps.arcgix.com/apps/webappviewer/index.html?id=LongMapID?token=" + token + "&amp;extent=1,-1,1,-1&amp;zoom=true&amp;scale=true&amp;search=true&amp;searchextent=true&amp;legend=true&amp;basemap_gallery=true&amp;disable_scroll=true&amp;theme=light" 
     } 
    } 
    xhttp.open("GET", "http://yournodeserver.com/get-a-token", true); 
    xhttp.send(); 
</script> 

あなたは/get-a-tokenルートを保護するために何かをしたい場合がありますあなた以外のサイトからのアクセスは禁止されています。

あなたはノード/あまりにも表現を使用してHTMLファイルを提供しているなら、あなたは

+0

残念ながら、console.logはinit.js:19に 'Uncaught Error:undefinedModule'と戻ってきています。また、' Uncaught ReferenceError:sendが定義されていません。 ' – user25730

+1

あなたが使っているコードを投稿できますか? console.logはエラーを記録せず、access_tokenの値だけをログに記録しますが、それをスローする他のコードが存在する可能性があります。 – carlevans719

+0

私は基本的に上記のコードを使用しています。しかし、node.jsを正しくインストールしたかどうかは不明です。 – user25730

関連する問題