使用以下基本节点 OAuth 重定向 [英] Working with basic Node OAuth redirect following

查看:44
本文介绍了使用以下基本节点 OAuth 重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仍然对节点的一点感到困惑.

我有一个在 http://localhost:3030 上运行的服务器来监听重定向的命中,但重定向永远不会到来.

在 Node 中,您如何实际上让请求遵循重定向.并最终在
http://localhost:3030/?code=ccf3d214669645f594b59be14032e20d>这是链接;在浏览器中,它确实会出现在正确的位置
https://www.instagram.com/oauth/authorize/?client_id=8901edf0746b460489427434ba5d321e&redirect_uri=http://localhost:3030&response_type=code

解决方案

我的第一个回答和评论仍然有效,但我将向您展示如何使用 nwjs.

首先,您需要在安全"选项卡下为您的 OAuth 应用启用隐式流程.

出于本演示的目的,我使用 http://localhost:3000/callback 作为我的 OAuth 应用程序的重定向 URI.因此,您需要将其添加为 OAuth 应用程序的附加重定向 URL.还要在 authorization.html 中填写所有必需的凭据.

server.js

var fs = require('fs')var path = require('path')var http = require('http')var url = require('url')var qs = require('querystring')var child = require('child_process')var nw = nullvar server = http.createServer()server.on('request', function (req, res) {if (req.url == '/connect') {var dpath = path.resolve(__dirname)nw = child.spawn('nw', [dpath])重发()}否则 if (req.url == '/callback') {var fpath = path.resolve(__dirname, 'token.html')var body = fs.readFileSync(fpath, 'utf8')res.writeHead(200, {'content-type': 'text/html'})res.end(body)}否则 if (/^\/token/.test(req.url)) {var uri = url.parse(req.url)var query = qs.parse(uri.query)控制台日志(查询)nw.on('close', function (code, signal) {console.log('西北关闭')})nw.kill('SIGHUP')重发()}})server.listen(3000,函数(){console.log('HTTP 服务器监听端口 ' + 3000)})

开始:

node server.js

然后导航到 http://localhost:3000/connect,您现在可以使用浏览器了.

授权.html

<头><meta http-equiv="Content-type" content="text/html; charset=utf-8"/><title>客户端隐式 OAuth 流程</title><script type="text/javascript" charset="utf-8">变量配置 = {client_id: '[CLIENT_ID]',redirect_uri: '[REDIRECT_URL]',用户名:'[用户名]',密码:'[密码]'}var authorize_url = 'https://www.instagram.com/oauth/authorize/?'+'client_id=' + config.client_id + '&'+'redirect_uri=' + config.redirect_uri + '&'+'响应类型=令牌'document.addEventListener('DOMContentLoaded', function (e) {var iframe = document.querySelector('iframe')iframe.setAttribute('src', authorize_url)iframe.onload = 函数 (e) {var doc = this.contentWindow.document//登录if (doc.querySelector('[name=username]')) {doc.querySelector('[name=username]').value = config.usernamedoc.querySelector('[name=password]').value = config.passworddoc.querySelector('[type=submit]').click()}//授权else if (doc.querySelector('[value=Authorize]')) {doc.querySelector('[value=Authorize]').click()}}}, 错误的)<身体><iframe src=""></iframe></html>

一旦您点击该路线,就会生成一个新进程并执行 authorize.html.请记住,NWjs 需要在您的服务器上安装一些图形库,因此它不完全是无头浏览器.

浏览器导航到 iframe 内的授权 URL.根据您是登录还是已经授权,应用程序会加载不同的页面.此代码只是填写您的用户名和密码,然后点击几个链接.

OAuth 流程完成后,您将获得 access_token 作为 URL 中的哈希值.您可能知道浏览器不会将 URL 的那部分发送到服务器,因此在 /callback 路由中,服务器返回另一个名为 token.html 的页面,该页面是唯一的目的是从 URL 哈希中提取访问令牌并将其作为 /token 路由中的查询字符串返回.

令牌.html

<头><meta http-equiv="Content-type" content="text/html; charset=utf-8"/><title>客户端隐式 OAuth 流程</title><script type="text/javascript" charset="utf-8">变量配置 = {callback_uri: 'http://localhost:3000/token'}var access_token = window.location.hash.replace('#access_token=', '')var url = config.callback_uri + '?access_token=' + access_tokenwindow.location.href = url<身体></html>

运行此示例后,您将在命令行中看到您的访问令牌:

$ node server.jsHTTP 服务器监听端口 3000{访问令牌:'1404767371.e5610d0.3381e9a2fd7340d8b90b729f407949d2'}西北关闭

您可以从此处下载所有文件.

Still confused about one point with node.

I’ve got a server running at http://localhost:3030 to listen for the hit from the redirect, but the redirect never comes.

How, in Node, do you have the request actually follow the redirects. And end up at
http://localhost:3030/?code=ccf3d214669645f594b59be14032e20d

Here is the link; In the browser it does end up in the right place
https://www.instagram.com/oauth/authorize/?client_id=8901edf0746b460489427434ba5d321e&redirect_uri=http://localhost:3030&response_type=code

解决方案

My first answer and comments are still valid, but I'm going to show you how you can automate the authorization process with nwjs.

First you need to enable the Implicit flow for your OAuth app - under the Security tab.

For the purpose of this demo I'm using http://localhost:3000/callback as redirect URI for my OAuth app. So you need to add it as additional redirect URL of your OAuth app. Also fill in all of the required credentials in authorization.html.

server.js

var fs = require('fs')
var path = require('path')
var http = require('http')
var url = require('url')
var qs = require('querystring')
var child = require('child_process')

var nw = null
var server = http.createServer()

server.on('request', function (req, res) {
  if (req.url == '/connect') {
    var dpath = path.resolve(__dirname)
    nw = child.spawn('nw', [dpath])
    res.end()
  }
  else if (req.url == '/callback') {
    var fpath = path.resolve(__dirname, 'token.html')
    var body = fs.readFileSync(fpath, 'utf8')
    res.writeHead(200, {'content-type': 'text/html'})
    res.end(body)
  }
  else if (/^\/token/.test(req.url)) {
    var uri = url.parse(req.url)
    var query = qs.parse(uri.query)
    console.log(query)
    nw.on('close', function (code, signal) {
      console.log('NW closed')
    })
    nw.kill('SIGHUP')
    res.end()
  }
})

server.listen(3000, function () {
  console.log('HTTP server listening on port ' + 3000)
})

Start it with:

node server.js

Then navigate to http://localhost:3000/connect, you can use your browser for now.

authorize.html

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  <title>Client Side Implicit OAuth Flow</title>
  <script type="text/javascript" charset="utf-8">
    var config = {
      client_id: '[CLIENT_ID]',
      redirect_uri: '[REDIRECT_URL]',
      username: '[USERNAME]',
      password: '[PASSWORD]'
    }

    var authorize_url = 'https://www.instagram.com/oauth/authorize/?' +
      'client_id=' + config.client_id + '&' +
      'redirect_uri=' + config.redirect_uri + '&' +
      'response_type=token'

    document.addEventListener('DOMContentLoaded', function (e) {
      var iframe = document.querySelector('iframe')
      iframe.setAttribute('src', authorize_url)
      iframe.onload = function (e) {
        var doc = this.contentWindow.document
        // login
        if (doc.querySelector('[name=username]')) {
          doc.querySelector('[name=username]').value = config.username
          doc.querySelector('[name=password]').value = config.password
          doc.querySelector('[type=submit]').click()
        }
        // authorize
        else if (doc.querySelector('[value=Authorize]')) {
          doc.querySelector('[value=Authorize]').click()
        }
      }
    }, false)
  </script>
</head>
<body>
  <iframe src=""></iframe>
</body>
</html>

Once you hit that route a new process is spawned and the authorize.html is executed. Just keep in mind that NWjs requires some graphical libraries to be installed on your server, so it's not exactly a headless browser.

There the browser navigates to the authorization URL inside an iframe. Depending on whether you are logged in or already authorized the app different pages are loaded. This code just fills in your user name and password and clicks on a few links.

Once the OAuth flow is complete you get the access_token as hash in the URL. As you may know the browser doesn't send that part of the URL to the server, so in the /callback route the server returns another page called token.html which sole purpose is to extract the access token from the URL hash and return it as a querystring in the /token route.

token.html

<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
  <title>Client Side Implicit OAuth Flow</title>
  <script type="text/javascript" charset="utf-8">
    var config = {
      callback_uri: 'http://localhost:3000/token'
    }

    var access_token = window.location.hash.replace('#access_token=', '')
    var url = config.callback_uri + '?access_token=' + access_token

    window.location.href = url
  </script>
</head>
<body>
</body>
</html>

After you run this example you'll see your access token in the command line:

$ node server.js 
HTTP server listening on port 3000
{ access_token: '1404767371.e5610d0.3381e9a2fd7340d8b90b729f407949d2' }
NW closed

You can download all files from here.

这篇关于使用以下基本节点 OAuth 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆