【问题标题】:Can't get response data with Oauth2/BaseCamp API无法使用 Oauth2/BaseCamp API 获取响应数据
【发布时间】:2019-11-28 14:11:43
【问题描述】:

我是一个使用 javascript API 的“高级初学者”,所以我的需求可能是二年级的,但是在我整晚撞墙之后,感谢您提供的任何基本指导。我正在尝试使用 Oauth2 对应用程序进行身份验证以访问我的 BaseCamp 站点。

我正在使用Grant Express 并成功拥有registered my app,这样我就收到了client_ID、client_secret 和redirect uri。对于重定向 uri,我添加了一个名为“auth”的文件夹,但其中唯一的内容是一个空白的 index.html 文件。所以重定向网址是http://example.com/auth

在我的本地机器上,我创建了一个名为 oauth 的目录,并在其中运行: npm 安装快递 npm install grant-express

我创建了一个文件 app.js,如下所示:

var express = require('express')
  , session = require('express-session')
var Grant = require('grant-express')

var config = {

  server: {
    protocol: "http",
    host: "127.0.0.1:3000"
    },
  basecamp: {
    key: "key_from_basecamp",
    secret: "secret_from_basecamp",
    callback: "/basecamp/callback"
  }
}

var app = express()
app.use(session({secret:'grant',
    resave: true,
    saveUninitialized: true}))
app.use(new Grant(config))   
app.get("/basecamp/callback", function (req, res) {
  console.log(req.query)
  res.end(JSON.stringify(req.query, null, 2))
})    
app.listen(3000, function () {
  console.log('Express server listening on port ' + 3000)
})

package.json 文件如下所示:

{
  "name": "auth",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "dependencies": {
    "express": "^4.13.4",
    "grant-express": "^3.6.0"
  },
  "devDependencies": {},
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

我去终端输入node app.js并得到响应:Express server listening on port 3000。没事吧?

当我在浏览器中输入http://localhost:3000/connect/basecamp 时,网址会重定向到:https://launchpad.37signals.com/authorization/new?client_id=client_id_from_basecamp&response_type=code&redirect_uri=http%3A%2F%2F127.0.0.1%3A3000%2Fconnect%2Fbasecamp%2Fcallback&type=web_server 但页面包含此错误::error: Provided redirect_uri is not approved

如果我得到:http://localhost:3000/connect/basecamp/callback 我看到这个错误(也在服务器的控制台中){ error: { error: 'Grant: OAuth2 missing code parameter' } }

Basecamp API documentation 它说: 使用您的 client_id、client_secret 和 redirect_uri 配置您的 OAuth 2 库。告诉 it 使用 https://launchpad.37signals.com/authorization/new 请求授权并使用 https://launchpad.37signals.com/authorization/token 获取访问令牌。什么是“它”以及我将如何告诉它使用这些网址?我是否将这些 url 作为对象添加到我的 app.js 文件中?还是我进入另一个文件?我需要在http://example.com/auth 中添加一些内容吗?

不知道从这里去哪里....非常感谢您的帮助。

【问题讨论】:

    标签: javascript oauth basecamp grant-oauth


    【解决方案1】:

    问题是我的重定向网址。它应该是 http://localhost:3000/connect/basecamp/callback 新的错误!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      • 2021-01-31
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多