UNPKG

2.46 kBMarkdownView Raw
1
2# Authentication
3
4## OAuth
5
6### OAuth1.0 Refresh Token
7
8- http://oauth.googlecode.com/svn/spec/ext/session/1.0/drafts/1/spec.html#anchor4
9- https://developer.yahoo.com/oauth/guide/oauth-refreshaccesstoken.html
10
11```js
12request.post('https://api.login.yahoo.com/oauth/v2/get_token', {
13 oauth: {
14 consumer_key: '...',
15 consumer_secret: '...',
16 token: '...',
17 token_secret: '...',
18 session_handle: '...'
19 }
20}, function (err, res, body) {
21 var result = require('querystring').parse(body)
22 // assert.equal(typeof result, 'object')
23})
24```
25
26### OAuth2 Refresh Token
27
28- https://tools.ietf.org/html/draft-ietf-oauth-v2-31#section-6
29
30```js
31request.post('https://accounts.google.com/o/oauth2/token', {
32 form: {
33 grant_type: 'refresh_token',
34 client_id: '...',
35 client_secret: '...',
36 refresh_token: '...'
37 },
38 json: true
39}, function (err, res, body) {
40 // assert.equal(typeof body, 'object')
41})
42```
43
44# Multipart
45
46## multipart/form-data
47
48### Flickr Image Upload
49
50- https://www.flickr.com/services/api/upload.api.html
51
52```js
53request.post('https://up.flickr.com/services/upload', {
54 oauth: {
55 consumer_key: '...',
56 consumer_secret: '...',
57 token: '...',
58 token_secret: '...'
59 },
60 // all meta data should be included here for proper signing
61 qs: {
62 title: 'My cat is awesome',
63 description: 'Sent on ' + new Date(),
64 is_public: 1
65 },
66 // again the same meta data + the actual photo
67 formData: {
68 title: 'My cat is awesome',
69 description: 'Sent on ' + new Date(),
70 is_public: 1,
71 photo:fs.createReadStream('cat.png')
72 },
73 json: true
74}, function (err, res, body) {
75 // assert.equal(typeof body, 'object')
76})
77```
78
79# Streams
80
81## `POST` data
82
83Use Request as a Writable stream to easily `POST` Readable streams (like files, other HTTP requests, or otherwise).
84
85TL;DR: Pipe a Readable Stream onto Request via:
86
87```
88READABLE.pipe(request.post(URL));
89```
90
91A more detailed example:
92
93```js
94var fs = require('fs')
95 , path = require('path')
96 , http = require('http')
97 , request = require('request')
98 , TMP_FILE_PATH = path.join(path.sep, 'tmp', 'foo')
99;
100
101// write a temporary file:
102fs.writeFileSync(TMP_FILE_PATH, 'foo bar baz quk\n');
103
104http.createServer(function(req, res) {
105 console.log('the server is receiving data!\n');
106 req
107 .on('end', res.end.bind(res))
108 .pipe(process.stdout)
109 ;
110}).listen(3000).unref();
111
112fs.createReadStream(TMP_FILE_PATH)
113 .pipe(request.post('http://127.0.0.1:3000'))
114;
115```