首页 > 解决方案 > 使用 ArcGIS JavaScript API 设置基于应用程序的登录

问题描述

我正在尝试将 ArcGIS 地图实施到现有的 JavaScript 应用程序 (Node.js/React) 中,并在尝试从 ArcGIS Online 加载图层时开始在我的应用程序中看到 ArcGIS 登录弹出窗口。由于并非所有用户都拥有 ArcGIS 帐户,因此我想启用基于应用程序的登录。

从文档(似乎它让我绕圈子)看起来我需要使用我的客户端 id/secret 设置我的 Nodejs 服务器,以便它可以获取访问令牌,然后将它们发送到客户端,以便客户端可以进入进而可以访问 ArcGIS Online 中的资源。

其中的服务器端部分似乎很简单——只需发出一个请求并取回一个有效的令牌。但是我不清楚一旦我的客户端应用程序从我的 Nodejs 服务器获取令牌后该怎么做。由于我的客户端代码是使用 React 编写的,因此我使用 @esri/react-arcgis npm 包来加载 ArcGIS 模块。我一直在玩 IdentityManager 模块,但没有成功。

如果有人知道如何设置基于应用程序的登录,我将不胜感激。这是我的客户端 React 代码。

import React from 'react';
import {loadModules} from '@esri/react-arcgis';
const options = {url: 'https://js.arcgis.com/4.6/'};

const styles = {
  container: {
    height: '100%',
    width: '100%'
  },
  mapDiv: {
    padding: 0,
    margin: 0,
    height: '100%',
    width: '100%'
  },
}

export default class MapTab extends React.Component {

    constructor(props) {
      super(props);
      this.state = {
        status: 'loading'
      }

      loadModules(['esri/Map', 'esri/views/MapView', 'esri/layers/MapImageLayer'], options)
        .then(([Map, MapView, MapImageLayer]) => {

          // how do I implement app based login here once I have the access token?

          var layer3 = new MapImageLayer({
            url: "https://livefeeds.arcgis.com/arcgis/rest/services/LiveFeeds/NWS_Watches_Warnings_and_Advisories/MapServer"
          });

          const map = new Map({
            basemap: "hybrid",
            layers: [layer3]
          });

          const view = new MapView({
            container: "viewDiv",
            map,
            center: [this.props.latLng.lng, this.props.latLng.lat],
            zoom: 5,
          });

          view.when(() => {
            this.setState({
              map,
              view,
              status: 'loaded'
            });
          });
        })

    }
        renderMap() {
          if(this.state.status === 'loading') {
            return <h1>loading</h1>;
          }
        }

        render() {
          if(this.state.view){
            this.state.view.goTo([this.props.latLng.lng, this.props.latLng.lat])
          }

          return(
                <div id='parent' style={styles.container}>
                  <div id='viewDiv' style={ styles.mapDiv } >
                    {this.renderMap()}
                  </div>
                </div>
          )
        }
      }

标签: javascriptarcgisarcgis-js-api

解决方案


请按照以下说明操作:https ://developers.arcgis.com/javascript/latest/guide/access-services-with-oauth-2/

但是快速总结一下:

  1. 转到https://developers.arcgis.com
  2. 使用您的 ArcGIS Online 帐户信息登录
  3. 转到“应用程序”并创建一个“新应用程序”:https ://developers.arcgis.com/applications
  4. 请务必将重定向 URI 更新到您的应用程序所在的位置,然后记下“客户端 ID”
  5. 在您的应用中,使用esri/identity/OAuthInfo将上一步中的 appId 放入您的代码中。

推荐阅读