ninjs-request
Version:
request engine
155 lines (129 loc) • 4.44 kB
JavaScript
'use strict'
const _ = require('ninjs-lodash')
const request = require('../')
const fs = require('fs-extra')
const http = require('http')
const _NAME = _.path.name(__filename)
const _NS = `ninjs.request.test.${_NAME}`
const _URL = 'https://google.com'
module.exports = {
NAME: _NAME,
NS: _NS,
DIR: __dirname,
FILE: __filename,
run: run,
get: get
}
function run(options) {
toFile()
toPut()
toPost()
getPut()
getPost()
getPutWithResponseEvent()
getPutWithErrorEvent()
httpTest()
advancedHttpTest()
piping()
}
// You can stream any response to a file stream.
function toFile() {
request('http://google.com/doodle.png')
.pipe(fs.createWriteStream('doodle.png'))
}
OAuth version 1.0 is supported. The default signing algorithm is HMAC-SHA1:
// OAuth1.0 - 3-legged server side flow (Twitter example)
// step 1
var qs = require('querystring')
, oauth =
{ callback: 'http://mysite.com/callback/'
, consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
}
, url = 'https://api.twitter.com/oauth/request_token'
;
request.post({url:url, oauth:oauth}, function (e, r, body) {
// Ideally, you would take the body in the response
// and construct a URL that a user clicks on (like a sign in button).
// The verifier is only available in the response after a user has
// verified with twitter that they are authorizing your app.
// step 2
var req_data = qs.parse(body)
var uri = 'https://api.twitter.com/oauth/authenticate'
+ '?' + qs.stringify({oauth_token: req_data.oauth_token})
// redirect the user to the authorize uri
// step 3
// after the user is redirected back to your server
var auth_data = qs.parse(body)
, oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
, token: auth_data.oauth_token
, token_secret: req_data.oauth_token_secret
, verifier: auth_data.oauth_verifier
}
, url = 'https://api.twitter.com/oauth/access_token'
;
request.post({url:url, oauth:oauth}, function (e, r, body) {
// ready to make signed requests on behalf of the user
var perm_data = qs.parse(body)
, oauth =
{ consumer_key: CONSUMER_KEY
, consumer_secret: CONSUMER_SECRET
, token: perm_data.oauth_token
, token_secret: perm_data.oauth_token_secret
}
, url = 'https://api.twitter.com/1.1/users/show.json'
, qs =
{ screen_name: perm_data.screen_name
, user_id: perm_data.user_id
}
;
request.get({url:url, oauth:oauth, qs:qs, json:true}, function (e, r, user) {
console.log(user)
})
})
})
For RSA-SHA1 signing, make the following changes to the OAuth options object:
Pass signature_method : 'RSA-SHA1'
Instead of consumer_secret, specify a private_key string in PEM format
For PLAINTEXT signing, make the following changes to the OAuth options object:
Pass signature_method : 'PLAINTEXT'
To send OAuth parameters via query params or in a post body as described in The Consumer Request Parameters section of the oauth1 spec:
Pass transport_method : 'query' or transport_method : 'body' in the OAuth options object.
transport_method defaults to 'header'
To use Request Body Hash you can either
Manually generate the body hash and pass it as a string body_hash: '...'
Automatically generate the body hash by passing body_hash: tru
- http://oauth.googlecode.com/svn/spec/ext/session/1.0/drafts/1/spec.html#anchor4
- https://developer.yahoo.com/oauth/guide/oauth-refreshaccesstoken.html
```js
request.post('https://api.login.yahoo.com/oauth/v2/get_token', {
oauth: {
consumer_key: '...',
consumer_secret: '...',
token: '...',
token_secret: '...',
session_handle: '...'
}
}, function (err, res, body) {
var result = require('querystring').parse(body)
// assert.equal(typeof result, 'object')
})
```
- https://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-6
```js
request.post('https://accounts.google.com/o/oauth2/token', {
form: {
grant_type: 'refresh_token',
client_id: '...',
client_secret: '...',
refresh_token: '...'
},
json: true
}, function (err, res, body) {
// assert.equal(typeof body, 'object')
})
```