【问题标题】:create oauth2 isn't working创建 oauth2 不起作用
【发布时间】:2017-08-10 22:41:39
【问题描述】:

我正在尝试使用 Outlook 邮件 API 在 Meteor 上构建一个 Web 应用程序。第一步是通过 Outlook 邮件 API 获取用户身份验证,这需要附加本地主机、客户端 ID、机密等的自定义 URL。

我查看了一些 node.js oauth2 教程并下载了 simple-oauth2。所有的教程都使用类似的东西:

 const oauth2 = require('oauth2').create({
  client: {
     id: '<client-ID>',
     secret: '<app-password>',
  },
  auth: {
     tokenHost: 'https://login.microsoftonline.com',
     authorizePath: 'common/oauth2/v2.0/authorize',
     tokenPath: 'common/oauth2/v2.0/token'
  }
});

当我运行它时,控制台会记录“Uncaught TypeError: require(...).create is not a function。” create() 是我没有的某种下载附带的东西吗?如果我将它分成两部分,require 可以正常工作,所以我知道这是 create() 的问题。

【问题讨论】:

    标签: node.js meteor oauth-2.0


    【解决方案1】:

    首先你列出 const 然后不要在最后扩展它们,只有你知道你在做什么。

    其次,我不使用 Outlook API,但我使用 OneDrive API,它应该是相似的。您需要遵循有关如何获取身份验证代码的文档。或者在这里我为 OneDrive 创建它的指南应该与您的问题相同

    https://apps.dev.microsoft.com/ 添加应用程序,您需要为此使用 microsoft 帐户。

    输入您的应用程序名称。在这种情况下,不要使用引导式设置

    创建应用程序密码并将其复制到保存位置

    添加平台并选择网络。在这种情况下,重定向 URL 是 http://localhost/dashboard

    转到下面的 Graph access 并选择 Files.ReadWrite.All 两个选项

    确保你按下保存键。

    在 xxxxxx 所在的 URL 中输入您的客户 ID。请注意,客户端 ID 是您可以在应用程序设置顶部找到的应用程序 ID。 https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=xxxxxxxxxx&scope=offline_access%20files.readwrite.all&response_type=code&redirect_uri=http://localhost/dashboard

    请将以上网址输入您的浏览器。使用您的 microsoft 帐户登录,然后单击“是”授予您的应用程序访问权限。然后你会被重定向到http://localhost/dashboard/?code=xxxxxxxxx-xxxxxx-xxxxx-xxx-xxxxx

    此验证码可用于将通过此步骤生成的 access_token

    request.post({url:'https://login.microsoftonline.com/common/oauth2/v2.0/token', form: {
        redirect_uri: 'https://login.live.com/oauth20_desktop.srf',
        client_id: 'xxxxxxxxxxxxxxxxx',
        client_secret: 'xxxxxxxxxxxxxxxx',
        code: 'xxxxxxxxxxxxxxxxxx',
        grant_type: 'authorization_code'
    }
    }, function(err,httpResponse,body){ /* ... */ 
    console.log('err: ' + err)
    console.log('body: ' + body)
    });
    

    对于每个 oAuth2 API,这基本上是相同的过程。请注意,我在 access_token 请求中为其他 API 使用了 redirect_uri http://localhost/dashboard

    如果您在以console.log 为主体发出请求并且您得到对象对象作为结果,那么请使用stringify。希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2020-10-20
      • 2014-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多