无法使用Oauth2 / BaseCamp API获取响应数据

时间:2016-04-02 15:53:35

标签: javascript oauth basecamp

我是一名高级初学者"使用javascript API,所以我的需求可能是二年级的,但是在我的头撞到墙上后,整夜都会欣赏你能提供的任何基本指导。我正在尝试使用Oauth2对我的BaseCamp网站进行身份验证。

我正在使用Grant Express并成功registered my app,因此我收到了client_ID,client_secret和重定向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库。告诉使用https://launchpad.37signals.com/authorization/new请求授权,https://launchpad.37signals.com/authorization/token访问令牌。什么是"它"以及如何,我会告诉它使用这些网址?我是否将这些网址作为对象添加到我的app.js文件中?或者我会进入另一个档案?我是否需要在http://example.com/auth中添加内容?

不确定从哪里开始......非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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