简体   繁体   English

在Nodejs中将Passport模块用于OAuth

[英]Using Passport module in Nodejs for OAuth

I am new to Node.js, OAuth. 我是OAuth的Node.js的新手。

I am developing a web app using Nodejs. 我正在使用Nodejs开发一个Web应用程序。 My application is interacting with the third party API to get data. 我的应用程序正在与第三方API交互以获取数据。 Per documentation of API "All methods in this third party API take OAUTH authentication parameters in request header"(that's it no more details) 根据API文档“此第三方API中的所有方法均在请求标头中使用OAUTH身份验证参数”(仅此而已,不再赘述)

I decided to use Passport module of Node.JS to achieve this( http://passportjs.org/guide/oauth/ ). 我决定使用Node.JS的Passport模块来实现此目的( http://passportjs.org/guide/oauth/ )。

My Problem is I am unable to understand how to proceed with this. 我的问题是我无法理解如何进行此操作。 Let say we have an API method " http://api.travel/getAllLocation " which returns all locations where user can travel. 假设我们有一个API方法“ http://api.travel/getAllLocation ”,该方法返回用户可以旅行的所有位置。

Can someone pls help here by providing example how to use NodeJS, Passport to get required data from webservice . 有人可以通过提供示例如何使用NodeJS,Passport从webservice获取所需数据的帮助来帮助您。

[Update] Have found this answer which is trying to achieve the same thing. [更新]找到了试图实现相同目的的答案。 But how to achieve the same using Passport How to send the OAuth request in Node 但是如何使用Passport实现相同的功能如何在Node中发送OAuth请求

It sounds to me as if you're trying to use Oauth to access someone else's API correct? 在我看来,您好像要尝试使用Oauth访问其他人的API正确吗? If that's the case, you need to interact with the API by doing this: 在这种情况下,您需要通过以下方式与API进行交互:

  • Requesting an Oauth token given an API key. 给定API密钥请求Oauth令牌。
  • Putting that token into your HTTP Authorization header so that the third-party API can identify and authenticate you. 将令牌放入您的HTTP授权标头中,以便第三方API可以识别和验证您的身份。

The way you can request an API token (typically) via the command line using cURL is as follows: 您可以使用cURL通过命令行(通常)请求API令牌的方式如下:

$ curl -v --user api_key_id:api_key_secret -X POST https://api.something.com/v1/auth?grant_type=client_credentials

This will typically return some sort of token (hopefully a JSON web token!) that you can then use to identify yourself. 通常,这将返回某种令牌(希望是JSON Web令牌!),然后您可以使用该令牌来标识自己。 Here's an example cURL request that properly specifies a token: 这是一个正确指定令牌的示例cURL请求:

$ curl -v -H "Authorization: Bearer tokenhere" https://api.something.com/v1/blah

Using Node, you can craft these HTTP requests with the https://github.com/request/request library. 使用Node,您可以使用https://github.com/request/request库制作这些HTTP请求。 Again, if you link us to the exact docs, I can help ya further =) 同样,如果您将我们链接到确切的文档,我可以进一步帮助您=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM