我试图创建一种用户可以简单地添加到网站的插件,它将对我的应用程序进行COR调用并返回将由客户端javascript处理的JSON。
这是我想要的方式,但现在我尝试确保用户登录我的应用程序,然后才允许从我的服务器端应用程序接收任何JSON。
从这里开始,我将我的Node.js API称为 Server ,将直接JS插件称为 Client
我找到了一个用于处理服务器上OAuth2的节点的npm插件,但我不确定我是否真正理解如何使用它。这是link,我发现this可以在客户端进行处理。
客户 - >应用初始化程序:
define [
'oauth2'
], (oauth2) ->
App =
Models: {}
Collections: {}
Views: {}
initialize: () ->
$.get "/javascripts/mu-config.json", (config) =>
@api_url = config.api
@site = config.site
@credentials = config.credentials
@make_oauth_call()
make_oauth_call: ->
@xhr = new oauth2.OAuth2XMLHttpRequest
authorizeEndpoint: "#{this.api_url}/callback"
tokenEndpoint: "#{this.api_url}/oauth/access_token"
clientID: this.credentials.clientID
clientSecret: this.credentials.clientSecret
localStoragePrefix: "oauth2.#{this.site.name}"
requestAuthorization: (callback) ->
console.log 'what?'
console.log callback
@xhr.onreadystatechange = () ->
console.log "do something"
@xhr.open "GET", "#{this.api_url}/notes?site=1&user=1"
@xhr.setRequestHeader 'Content-type', 'application/x-www-form-urlencoded'
@xhr.send "site=1&user=1"
那么这里有什么作用?那么@xhr.open ...
实际上从服务器中获取了JSON,但那是关于它的。我没有收到客户的任何错误,但console.log 'what?'
没有触发,我也不相信任何事情都会通过验证。
服务器 - > oauth.coffee
token = null
credentials =
clientID: "sparkmasterflex"
clientSecret: "bob_the_builder"
site: 'http://marking_up.dev'
OAuth2 = require('simple-oauth2') credentials
authorization_uri = OAuth2.AuthCode.authorizeURL
redirect_uri: 'http://localhost:3000/callback'
scope: 'sites'
state: '55fce6241c8e6432e8dfee583141aa58'
res.redirect(authorization_uri)
OAuth2.AuthCode.getToken
code: "something here"
redirect_uri: "http://localhost:3000/callback"
, saveToken
saveToken = (error, result) ->
console.log('Access Token Error', error.message) if error
token = OAuth2.AccessToken.create(result)
module.exports = OAuth2
服务器 - >路由器
express = require("express")
db = require "../database"
oauth2 = require "../oauth"
router = express.Router()
# GET home page.
router.get "/", (req, res) ->
res.render 'index',
title: "Hello world"
# Initial page redirecting to Github
router.get '/auth', (req, res) ->
res.redirect authorization_uri
# Callback service parsing the authorization token and asking for the access token
# router.get '/callback', (req, res) ->
router.route('/callback')
.get (req, res) ->
code = req.query.code
console.log '/callback'
oauth2.AuthCode.getToken
code: code
redirect_uri: 'http://localhost:3000/callback'
, saveToken
saveToken = (error, result) ->
console.log('Access Token Error', error.message) if error
token = oauth2.AccessToken.create(result)
module.exports = router
运行节点服务器我收到此错误:
/Users/raymondke99/Sites/marking_up_api/oauth.js:19
res.redirect(authorization_uri);
^
ReferenceError: res is not defined
at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/oauth.js:19:1)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/routes/index.js:7:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
我在这里有点不知所措。这两个文档看起来相当彻底,但我仍然觉得我错过了大量的信息。任何人都可以帮助和/或引导我提供帮助吗?
谢谢
修改
我从oauth.coffee中删除了res.redirect()
,我收到以下错误:
/Users/raymondke99/Sites/marking_up_api/node_modules/simple-oauth2/lib/core.js:16
throw new Error('Callback not provided on API call');
^
Error: Callback not provided on API call
at Object.api (/Users/raymondke99/Sites/marking_up_api/node_modules/simple-oauth2/lib/core.js:16:13)
at Object.getToken (/Users/raymondke99/Sites/marking_up_api/node_modules/simple-oauth2/lib/client/auth-code.js:34:8)
at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/oauth.js:19:17)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Users/raymondke99/Sites/marking_up_api/routes/index.js:7:10)
我有多台路由器,因为我使用的是快递,而且我不确定我应该在哪里使用#catch-all&#39;重定向。是否需要进入每个路由器?
答案 0 :(得分:0)
为什么你有&#34; res.redirect(authorization_uri)&#34;在誓言文件?您似乎已经在路由器中拥有GET端点了?