使用 OAuth 2.0 访问 GeoScene Online 项目

在线预览

这个示例向您展示了如何使用内联 web 流而未使用弹出窗口呈现登录用户界面。内联 web 流不包含弹出窗口。它允许用户使用直接内置在 IdentityManager 中的 OAuth 2.0 功能登录到 GeoScene Online 。

当您实施此类身份验证时,此内置功能必须执行的许多细粒度作业。

IdentityManager 组件通过将令牌附加到请求并在必要时获取新的令牌,从而简化了处理令牌的过程。你需要做的就是创建一个 OAuthInfo 对象,并指定你注册应用时收到的 appId 。接下来,将 OAuthInfo 对象传递给 IdentityManager  的 registerOauthInfos 方法,由 IdentityManager 负责其余的工作。

在这个示例中,有四个模块需要关注,分别是:

    
1
2
3
4
"geoscene/portal/Portal",
"geoscene/identity/OAuthInfo",
"geoscene/identity/IdentityManager",
"geoscene/portal/PortalQueryParams";
  1. 第一步是创建一个 OAuthInfo 对象 ,并将其注册到 IdentityManager 。

               
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    const info = new OAuthInfo({
      // Swap this ID out with registered application ID
      appId: "q244Lb8gDRgWQ8hM",
      // Uncomment the next line and update if using your own portal
      // portalUrl: "https://<host>:<port>/arcgis"
      // Uncomment the next line to prevent the user's signed in state from being shared with other apps on the same domain with the same authNamespace value.
      // authNamespace: "portal_oauth_inline",
      popup: false
    });
    
    geosceneId.registerOAuthInfos([info]);
    
  2. 接下来,检查用户是否登录。

          
    1
    2
    3
    4
    5
    6
    geosceneId
      .checkSignInStatus(info.portalUrl + "/sharing")
      .then(() => {
        displayItems();
      })
      .catch(/*give user an option to sign in*/);
  3. 登录后,检索凭证。

     
    1
    geosceneId.getCredential(info.portalUrl + "/sharing");

    若用户退出时,则摧毁凭证。这将撤销通过 OAuth 生成的任何令牌

     
    1
    geosceneId.destroyCredentials();
  4. 用户登录后,查询 Portal 中的项目。

                    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    const portal = new Portal();
    // Setting authMode to immediate signs the user in once loaded
    portal.authMode = "immediate";
    // Once loaded, user is signed in
    portal.load().then(() => {
      // Create query parameters for the portal search
      const queryParams = new PortalQueryParams({
        query: "owner:" + portal.user.username,
        sortField: "numViews",
        sortOrder: "desc",
        num: 20
      });
    
      // Query the items based on the queryParams created from portal above
      portal.queryItems(queryParams).then(createGallery);
    });
    

有关用户登录和 OAuth 2.0 的更多详细信息,请参阅 身份验证文档 。此外,请参考 GeoScene 官方网站 获取更多关于认证的信息。

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.