twitter-lite
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -1,2 +0,2 @@ | ||
var t=require("crypto"),e=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),i=require("./stream"),o=function(t){return"https://"+t+".twitter.com/1.1"},s={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u={"Content-Type":"application/json",Accept:"application/json"},c=function(r){var n,i=Object.assign({},s,r);this.authType=i.bearer_token?"App":"User",this.client=e({consumer:{key:(n={key:i.consumer_key,secret:i.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(e,r){return t.createHmac("sha1",r).update(e).digest("base64")}}),this.token={key:i.access_token_key,secret:i.access_token_secret},this.url=o(i.subdomain),this.config=i};c.prototype.get=function(t,e){return new Promise(function(i,o){var s,u;return s={url:this.url+"/"+t+".json",method:"GET"},e&&(s.url+="?"+n.stringify(e)),u={},u="User"===this.authType?this.client.toHeader(this.client.authorize(s,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(s.url,{headers:u}).then(function(t){return t.json()}).then(function(t){try{return i(t)}catch(t){return o(t)}}.bind(this),o)}.bind(this))},c.prototype.post=function(t,e,i){return new Promise(function(o,s){var c,a;return c={url:this.url+"/"+t+".json",method:"POST"},i&&(c.url+="?"+n.stringify(i)),a={},a="User"===this.authType?this.client.toHeader(this.client.authorize(c,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(c.url,{method:"POST",headers:Object.assign({},u,a),body:JSON.stringify(e)}).then(function(t){return t.json()}).then(function(t){try{return o(t)}catch(t){return s(t)}}.bind(this),s)}.bind(this))},c.prototype.stream=function(t,e){var s=this;if("User"!==this.authType)throw Error("Streams require user context authentication");var u=new i,c={url:o("stream")+"/"+t+".json",method:"GET"};e&&(c.url+="?"+n.stringify(e));var a=this.client.toHeader(this.client.authorize(c,this.token));return r(c.url,{headers:a}).then(function(t){s.stream.destroy=function(){return t.body.destroy()},200===t.status?u.emit("start",t):u.emit("error",Error("Status Code: "+t.status)),t.body.on("data",function(t){return u.parse(t)}).on("error",function(t){return u.emit("error",t)}).on("end",function(){return u.emit("end",t)})}).catch(function(t){return u.emit("error",t)}),u},module.exports=c; | ||
var t=require("crypto"),e=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),i=require("./stream"),o=function(t,e){return void 0===e&&(e="1.1"),"https://"+t+".twitter.com/"+e},s={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u={"Content-Type":"application/json",Accept:"application/json"},c=function(r){var n,i=Object.assign({},s,r);this.authType=i.bearer_token?"App":"User",this.client=e({consumer:{key:(n={key:i.consumer_key,secret:i.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(e,r){return t.createHmac("sha1",r).update(e).digest("base64")}}),this.token={key:i.access_token_key,secret:i.access_token_secret},this.url=o(i.subdomain),this.oauth=o(i.subdomain,"oauth"),this.config=i};c.prototype.getRequestToken=function(t){return new Promise(function(e,i){var o,s,c;return o={url:this.oauth+"/request_token",method:"POST"},s={},t&&(s={oauth_callback:t}),s&&(o.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(o,{})),r(o.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return i(t)}}.bind(this),i)}.bind(this))},c.prototype.getAccessToken=function(t){return new Promise(function(e,i){var o,s,c;return o={url:this.oauth+"/access_token",method:"POST"},(s={oauth_verifier:t.verifier})&&(o.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(o,{key:t.key,secret:t.secret})),r(o.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return i(t)}}.bind(this),i)}.bind(this))},c.prototype.get=function(t,e){return new Promise(function(i,o){var s,u;return s={url:this.url+"/"+t+".json",method:"GET"},e&&(s.url+="?"+n.stringify(e)),u={},u="User"===this.authType?this.client.toHeader(this.client.authorize(s,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(s.url,{headers:u}).then(function(t){return t.json()}).then(function(t){try{return i(t)}catch(t){return o(t)}}.bind(this),o)}.bind(this))},c.prototype.post=function(t,e,i){return new Promise(function(o,s){var c,h;return c={url:this.url+"/"+t+".json",method:"POST"},i&&(c.url+="?"+n.stringify(i)),h={},h="User"===this.authType?this.client.toHeader(this.client.authorize(c,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(c.url,{method:"POST",headers:Object.assign({},u,h),body:JSON.stringify(e)}).then(function(t){return t.json()}).then(function(t){try{return o(t)}catch(t){return s(t)}}.bind(this),s)}.bind(this))},c.prototype.stream=function(t,e){var s=this;if("User"!==this.authType)throw Error("Streams require user context authentication");var u=new i,c={url:o("stream")+"/"+t+".json",method:"GET"};e&&(c.url+="?"+n.stringify(e));var h=this.client.toHeader(this.client.authorize(c,this.token));return r(c.url,{headers:h}).then(function(t){s.stream.destroy=function(){return t.body.destroy()},200===t.status?u.emit("start",t):u.emit("error",Error("Status Code: "+t.status)),t.body.on("data",function(t){return u.parse(t)}).on("error",function(t){return u.emit("error",t)}).on("end",function(){return u.emit("end",t)})}).catch(function(t){return u.emit("error",t)}),u},module.exports=c; | ||
//# sourceMappingURL=twitter.js.map |
@@ -1,2 +0,2 @@ | ||
var t=require("crypto"),e=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),i=require("./stream"),o=function(t){return"https://"+t+".twitter.com/1.1"},s={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u={"Content-Type":"application/json",Accept:"application/json"},c=function(r){var n,i=Object.assign({},s,r);this.authType=i.bearer_token?"App":"User",this.client=e({consumer:{key:(n={key:i.consumer_key,secret:i.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(e,r){return t.createHmac("sha1",r).update(e).digest("base64")}}),this.token={key:i.access_token_key,secret:i.access_token_secret},this.url=o(i.subdomain),this.config=i};c.prototype.get=function(t,e){return new Promise(function(i,o){var s,u;return s={url:this.url+"/"+t+".json",method:"GET"},e&&(s.url+="?"+n.stringify(e)),u={},u="User"===this.authType?this.client.toHeader(this.client.authorize(s,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(s.url,{headers:u}).then(function(t){return t.json()}).then(function(t){try{return i(t)}catch(t){return o(t)}}.bind(this),o)}.bind(this))},c.prototype.post=function(t,e,i){return new Promise(function(o,s){var c,a;return c={url:this.url+"/"+t+".json",method:"POST"},i&&(c.url+="?"+n.stringify(i)),a={},a="User"===this.authType?this.client.toHeader(this.client.authorize(c,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(c.url,{method:"POST",headers:Object.assign({},u,a),body:JSON.stringify(e)}).then(function(t){return t.json()}).then(function(t){try{return o(t)}catch(t){return s(t)}}.bind(this),s)}.bind(this))},c.prototype.stream=function(t,e){var s=this;if("User"!==this.authType)throw Error("Streams require user context authentication");var u=new i,c={url:o("stream")+"/"+t+".json",method:"GET"};e&&(c.url+="?"+n.stringify(e));var a=this.client.toHeader(this.client.authorize(c,this.token));return r(c.url,{headers:a}).then(function(t){s.stream.destroy=function(){return t.body.destroy()},200===t.status?u.emit("start",t):u.emit("error",Error("Status Code: "+t.status)),t.body.on("data",function(t){return u.parse(t)}).on("error",function(t){return u.emit("error",t)}).on("end",function(){return u.emit("end",t)})}).catch(function(t){return u.emit("error",t)}),u},module.exports=c; | ||
var t=require("crypto"),e=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),i=require("./stream"),o=function(t,e){return void 0===e&&(e="1.1"),"https://"+t+".twitter.com/"+e},s={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u={"Content-Type":"application/json",Accept:"application/json"},c=function(r){var n,i=Object.assign({},s,r);this.authType=i.bearer_token?"App":"User",this.client=e({consumer:{key:(n={key:i.consumer_key,secret:i.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(e,r){return t.createHmac("sha1",r).update(e).digest("base64")}}),this.token={key:i.access_token_key,secret:i.access_token_secret},this.url=o(i.subdomain),this.oauth=o(i.subdomain,"oauth"),this.config=i};c.prototype.getRequestToken=function(t){return new Promise(function(e,i){var o,s,c;return o={url:this.oauth+"/request_token",method:"POST"},s={},t&&(s={oauth_callback:t}),s&&(o.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(o,{})),r(o.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return i(t)}}.bind(this),i)}.bind(this))},c.prototype.getAccessToken=function(t){return new Promise(function(e,i){var o,s,c;return o={url:this.oauth+"/access_token",method:"POST"},(s={oauth_verifier:t.verifier})&&(o.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(o,{key:t.key,secret:t.secret})),r(o.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return i(t)}}.bind(this),i)}.bind(this))},c.prototype.get=function(t,e){return new Promise(function(i,o){var s,u;return s={url:this.url+"/"+t+".json",method:"GET"},e&&(s.url+="?"+n.stringify(e)),u={},u="User"===this.authType?this.client.toHeader(this.client.authorize(s,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(s.url,{headers:u}).then(function(t){return t.json()}).then(function(t){try{return i(t)}catch(t){return o(t)}}.bind(this),o)}.bind(this))},c.prototype.post=function(t,e,i){return new Promise(function(o,s){var c,h;return c={url:this.url+"/"+t+".json",method:"POST"},i&&(c.url+="?"+n.stringify(i)),h={},h="User"===this.authType?this.client.toHeader(this.client.authorize(c,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(c.url,{method:"POST",headers:Object.assign({},u,h),body:JSON.stringify(e)}).then(function(t){return t.json()}).then(function(t){try{return o(t)}catch(t){return s(t)}}.bind(this),s)}.bind(this))},c.prototype.stream=function(t,e){var s=this;if("User"!==this.authType)throw Error("Streams require user context authentication");var u=new i,c={url:o("stream")+"/"+t+".json",method:"GET"};e&&(c.url+="?"+n.stringify(e));var h=this.client.toHeader(this.client.authorize(c,this.token));return r(c.url,{headers:h}).then(function(t){s.stream.destroy=function(){return t.body.destroy()},200===t.status?u.emit("start",t):u.emit("error",Error("Status Code: "+t.status)),t.body.on("data",function(t){return u.parse(t)}).on("error",function(t){return u.emit("error",t)}).on("end",function(){return u.emit("end",t)})}).catch(function(t){return u.emit("error",t)}),u},module.exports=c; | ||
//# sourceMappingURL=twitter.m.js.map |
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t():"function"==typeof define&&define.amd?define(t):t()}(0,function(){var e=require("crypto"),t=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),i=require("./stream"),o=function(e){return"https://"+e+".twitter.com/1.1"},s={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u={"Content-Type":"application/json",Accept:"application/json"},c=function(r){var n,i=Object.assign({},s,r);this.authType=i.bearer_token?"App":"User",this.client=t({consumer:{key:(n={key:i.consumer_key,secret:i.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(t,r){return e.createHmac("sha1",r).update(t).digest("base64")}}),this.token={key:i.access_token_key,secret:i.access_token_secret},this.url=o(i.subdomain),this.config=i};c.prototype.get=function(e,t){return new Promise(function(i,o){var s,u;return s={url:this.url+"/"+e+".json",method:"GET"},t&&(s.url+="?"+n.stringify(t)),u={},u="User"===this.authType?this.client.toHeader(this.client.authorize(s,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(s.url,{headers:u}).then(function(e){return e.json()}).then(function(e){try{return i(e)}catch(e){return o(e)}}.bind(this),o)}.bind(this))},c.prototype.post=function(e,t,i){return new Promise(function(o,s){var c,a;return c={url:this.url+"/"+e+".json",method:"POST"},i&&(c.url+="?"+n.stringify(i)),a={},a="User"===this.authType?this.client.toHeader(this.client.authorize(c,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(c.url,{method:"POST",headers:Object.assign({},u,a),body:JSON.stringify(t)}).then(function(e){return e.json()}).then(function(e){try{return o(e)}catch(e){return s(e)}}.bind(this),s)}.bind(this))},c.prototype.stream=function(e,t){var s=this;if("User"!==this.authType)throw Error("Streams require user context authentication");var u=new i,c={url:o("stream")+"/"+e+".json",method:"GET"};t&&(c.url+="?"+n.stringify(t));var a=this.client.toHeader(this.client.authorize(c,this.token));return r(c.url,{headers:a}).then(function(e){s.stream.destroy=function(){return e.body.destroy()},200===e.status?u.emit("start",e):u.emit("error",Error("Status Code: "+e.status)),e.body.on("data",function(e){return u.parse(e)}).on("error",function(e){return u.emit("error",e)}).on("end",function(){return u.emit("end",e)})}).catch(function(e){return u.emit("error",e)}),u},module.exports=c}); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e():"function"==typeof define&&define.amd?define(e):e()}(0,function(){var t=require("crypto"),e=require("oauth-1.0a"),r=require("cross-fetch"),n=require("querystring"),i=require("./stream"),o=function(t,e){return void 0===e&&(e="1.1"),"https://"+t+".twitter.com/"+e},s={subdomain:"api",consumer_key:null,consumer_secret:null,access_token_key:null,access_token_secret:null,bearer_token:null},u={"Content-Type":"application/json",Accept:"application/json"},c=function(r){var n,i=Object.assign({},s,r);this.authType=i.bearer_token?"App":"User",this.client=e({consumer:{key:(n={key:i.consumer_key,secret:i.consumer_secret}).key,secret:n.secret},signature_method:"HMAC-SHA1",hash_function:function(e,r){return t.createHmac("sha1",r).update(e).digest("base64")}}),this.token={key:i.access_token_key,secret:i.access_token_secret},this.url=o(i.subdomain),this.oauth=o(i.subdomain,"oauth"),this.config=i};c.prototype.getRequestToken=function(t){return new Promise(function(e,i){var o,s,c;return o={url:this.oauth+"/request_token",method:"POST"},s={},t&&(s={oauth_callback:t}),s&&(o.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(o,{})),r(o.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return i(t)}}.bind(this),i)}.bind(this))},c.prototype.getAccessToken=function(t){return new Promise(function(e,i){var o,s,c;return o={url:this.oauth+"/access_token",method:"POST"},(s={oauth_verifier:t.verifier})&&(o.url+="?"+n.stringify(s)),{},c=this.client.toHeader(this.client.authorize(o,{key:t.key,secret:t.secret})),r(o.url,{method:"POST",headers:Object.assign({},u,c)}).then(function(t){return t.text()}).then(function(t){return n.parse(t)}).then(function(t){try{return e(t)}catch(t){return i(t)}}.bind(this),i)}.bind(this))},c.prototype.get=function(t,e){return new Promise(function(i,o){var s,u;return s={url:this.url+"/"+t+".json",method:"GET"},e&&(s.url+="?"+n.stringify(e)),u={},u="User"===this.authType?this.client.toHeader(this.client.authorize(s,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(s.url,{headers:u}).then(function(t){return t.json()}).then(function(t){try{return i(t)}catch(t){return o(t)}}.bind(this),o)}.bind(this))},c.prototype.post=function(t,e,i){return new Promise(function(o,s){var c,a;return c={url:this.url+"/"+t+".json",method:"POST"},i&&(c.url+="?"+n.stringify(i)),a={},a="User"===this.authType?this.client.toHeader(this.client.authorize(c,this.token)):{Authorization:"Bearer "+this.config.bearer_token},r(c.url,{method:"POST",headers:Object.assign({},u,a),body:JSON.stringify(e)}).then(function(t){return t.json()}).then(function(t){try{return o(t)}catch(t){return s(t)}}.bind(this),s)}.bind(this))},c.prototype.stream=function(t,e){var s=this;if("User"!==this.authType)throw Error("Streams require user context authentication");var u=new i,c={url:o("stream")+"/"+t+".json",method:"GET"};e&&(c.url+="?"+n.stringify(e));var a=this.client.toHeader(this.client.authorize(c,this.token));return r(c.url,{headers:a}).then(function(t){s.stream.destroy=function(){return t.body.destroy()},200===t.status?u.emit("start",t):u.emit("error",Error("Status Code: "+t.status)),t.body.on("data",function(t){return u.parse(t)}).on("error",function(t){return u.emit("error",t)}).on("end",function(){return u.emit("end",t)})}).catch(function(t){return u.emit("error",t)}),u},module.exports=c}); | ||
//# sourceMappingURL=twitter.umd.js.map |
{ | ||
"name": "twitter-lite", | ||
"version": "0.4.0", | ||
"description": "A tiny, full-featured client / server library for the Twitter API", | ||
"source": [ | ||
"twitter.js", | ||
"stream.js" | ||
], | ||
"version": "0.5.0", | ||
"description": | ||
"A tiny, full-featured client / server library for the Twitter API", | ||
"source": ["twitter.js", "stream.js"], | ||
"main": "dist/twitter.js", | ||
@@ -15,7 +13,3 @@ "module": "dist/twitter.m.js", | ||
"license": "MIT", | ||
"files": [ | ||
"twitter.js", | ||
"stream.js", | ||
"dist" | ||
], | ||
"files": ["twitter.js", "stream.js", "dist"], | ||
"keywords": [ | ||
@@ -36,4 +30,8 @@ "twitter", | ||
"devDependencies": { | ||
"flow-bin": "^0.68.0", | ||
"husky": "^0.14.3", | ||
"jest": "^22.3.0", | ||
"microbundle": "^0.4.3" | ||
"lint-staged": "^7.0.0", | ||
"microbundle": "^0.4.3", | ||
"prettier": "^1.11.1" | ||
}, | ||
@@ -43,4 +41,9 @@ "scripts": { | ||
"test": "jest", | ||
"release": "npm run -s prepare && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" | ||
"release": | ||
"npm run -s prepare && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish", | ||
"precommit": "lint-staged" | ||
}, | ||
"lint-staged": { | ||
"*.{js,json,css,md}": ["prettier --write", "git add"] | ||
} | ||
} |
124
README.md
@@ -1,16 +0,21 @@ | ||
# Twitter Lite [![npm](https://img.shields.io/npm/v/twitter-lite.svg)](https://npm.im/twitter-lite) [![travis](https://travis-ci.org/Preposterous/twitter-lite.svg?branch=master)](https://travis-ci.org/Preposterous/twitter-lite) | ||
# Twitter Lite | ||
A tiny (591B), fully-featured, client & server library for the Twitter API | ||
A tiny Nodejs library for the Twitter API | ||
- Promise-first via Async / Await | ||
- Up-to-date | ||
- Under 1kb | ||
[![npm](https://img.shields.io/npm/v/twitter-lite.svg)](https://npm.im/twitter-lite) [![travis](https://travis-ci.org/Preposterous/twitter-lite.svg?branch=master)](https://travis-ci.org/Preposterous/twitter-lite) | ||
## Features | ||
* Promise driven via Async / Await | ||
* Up-to-date APIs | ||
* Stream support | ||
* Under 1kb | ||
## Installation | ||
``` | ||
```zsh | ||
yarn add twitter-lite | ||
``` | ||
``` | ||
```zsh | ||
npm install twitter-lite | ||
@@ -21,5 +26,5 @@ ``` | ||
- Create an app on [https://apps.twitter.com/](https://apps.twitter.com) | ||
- Grab the Consumer Key (API Key) and Consumer Secret (API Secret) from Keys and Access Tokens | ||
- Make sure you set the right access level for your app | ||
* Create an app on [https://apps.twitter.com/](https://apps.twitter.com) | ||
* Grab the Consumer Key (API Key) and Consumer Secret (API Secret) from Keys and Access Tokens | ||
* Make sure you set the right access level for your app | ||
@@ -29,14 +34,17 @@ ### App vs. User | ||
Twitter has two different authentication options: | ||
- App: higher rate limits. Great for building your own Twitter App | ||
- User: lower rate limits. Great for making requests on behalf of a User. | ||
* App: higher rate limits. Great for building your own Twitter App | ||
* User: lower rate limits. Great for making requests on behalf of a User. | ||
**User** authentication requires: | ||
- `consumer_key` | ||
- `consumer_secret` | ||
- `access_token_key` | ||
- `access_token_secret` | ||
* `consumer_key` | ||
* `consumer_secret` | ||
* `access_token_key` | ||
* `access_token_secret` | ||
**App** authentication requires: | ||
- `bearer_token` | ||
* `bearer_token` | ||
App authentication is a simple header behind the scenes: | ||
@@ -46,6 +54,50 @@ | ||
headers: { | ||
Authorization: `Bearer ${bearer_token}` | ||
Authorization: `Bearer ${bearer_token}`; | ||
} | ||
``` | ||
### Oauth Authentication | ||
According to the [docs](https://developer.twitter.com/en/docs/basics/authentication/api-reference/authenticate) this helps you get access token from your users. | ||
- [Request Token documentation](https://developer.twitter.com/en/docs/basics/authentication/api-reference/request_token) | ||
- [Access Token documentation](https://developer.twitter.com/en/docs/basics/authentication/api-reference/access_token) | ||
```es6 | ||
const client = new Twitter({ | ||
consumer_key: "xyz", | ||
consumer_secret: "xyz" | ||
}); | ||
client.getRequestToken("http://callbackurl.com") | ||
.then(res => console.log({ | ||
reqTkn: res.oauth_token, | ||
reqTknSecret: res.oauth_token_secret | ||
})) | ||
.catch(console.error); | ||
``` | ||
Then you redirect your user to `https://api.twitter.com/oauth/authenticate?oauth_token=xyz123abc`, and once you get the verifier and the token, you pass them on to the next stage of the authentication. | ||
```es6 | ||
const client = new Twitter({ | ||
consumer_key: "xyz", | ||
consumer_secret: "xyz" | ||
}); | ||
client.getAccessToken({ | ||
key: requestToken, | ||
secret: requestTokenSecret, | ||
verifier: oauthVerifier | ||
}) | ||
.then(res=>console.log({ | ||
accTkn: res.oauth_token, | ||
accTknSecret: res.oauth_token_secret, | ||
userId: res.user_id, | ||
screenName: res.screen_name | ||
})) | ||
.catch(console.error); | ||
``` | ||
And this will return you your `access_token` and `access_token_secret`. | ||
### Verifying Credentials Example (User auth) | ||
@@ -60,7 +112,10 @@ | ||
access_token_secret: "abc" // from your User (oauth_token_secret) | ||
}) | ||
}); | ||
client.get("account/verify_credentials") | ||
.then(results => { console.log("results", results) }) | ||
.catch(console.error) | ||
client | ||
.get("account/verify_credentials") | ||
.then(results => { | ||
console.log("results", results); | ||
}) | ||
.catch(console.error); | ||
``` | ||
@@ -74,7 +129,10 @@ | ||
bearer_token: "Bearer ABC123XYZ" // generate a Bearer token | ||
}) | ||
}); | ||
client.get("users/lookup") | ||
.then(results => { console.log("results", results ) }) | ||
.catch(console.error) | ||
client | ||
.get("users/lookup") | ||
.then(results => { | ||
console.log("results", results); | ||
}) | ||
.catch(console.error); | ||
``` | ||
@@ -117,5 +175,5 @@ | ||
Thus errors described in twitter docs for example as: | ||
```JSON | ||
{ "errors": [ { "code": 88, "message": "Rate limit exceeded" } ] } | ||
``` | ||
@@ -126,4 +184,3 @@ | ||
``` | ||
[ { "code": 88, "message": "Rate limit exceeded" } ] | ||
[ { "code": 88, "message": "Rate limit exceeded" } ] | ||
``` | ||
@@ -134,5 +191,6 @@ | ||
Over the years, thanks to: | ||
- [@technoweenie](http://github.com/technoweenie) | ||
- [@jdub](http://github.com/jdub) | ||
- [@desmondmorris](http://github.com/desmondmorris) | ||
- [Node Twitter Community](https://github.com/desmondmorris/node-twitter/graphs/contributors) | ||
* [@technoweenie](http://github.com/technoweenie) | ||
* [@jdub](http://github.com/jdub) | ||
* [@desmondmorris](http://github.com/desmondmorris) | ||
* [Node Twitter Community](https://github.com/desmondmorris/node-twitter/graphs/contributors) |
@@ -15,3 +15,3 @@ const EventEmitter = require("events"); | ||
let json; | ||
while ((index = this.buffer.indexOf(END)) > -1) { | ||
@@ -23,3 +23,3 @@ json = this.buffer.slice(0, index); | ||
json = JSON.parse(json); | ||
this.emit(json.event || "data", json) | ||
this.emit(json.event || "data", json); | ||
} catch (error) { | ||
@@ -36,2 +36,2 @@ error.source = json; | ||
module.exports = Stream; | ||
module.exports = Stream; |
@@ -7,3 +7,4 @@ const crypto = require("crypto"); | ||
const getUrl = subdomain => `https://${subdomain}.twitter.com/1.1`; | ||
const getUrl = (subdomain, endpoint='1.1') => `https://${subdomain}.twitter.com/${endpoint}`; | ||
const createOauthClient = ({ key, secret }) => { | ||
@@ -53,5 +54,58 @@ const client = OAuth({ | ||
this.url = getUrl(config.subdomain); | ||
this.oauth = getUrl(config.subdomain, "oauth"); | ||
this.config = config; | ||
} | ||
async getRequestToken(twitterCallbackUrl) { | ||
const requestData = { | ||
url: `${this.oauth}/request_token`, | ||
method: "POST" | ||
}; | ||
var parameters = {}; | ||
if(twitterCallbackUrl) parameters = { "oauth_callback": twitterCallbackUrl }; | ||
if (parameters) requestData.url += "?" + querystring.stringify(parameters); | ||
let headers = {}; | ||
headers = this.client.toHeader( | ||
this.client.authorize(requestData, {}) | ||
); | ||
const results = await Fetch(requestData.url, { | ||
method: "POST", | ||
headers: Object.assign({}, baseHeaders, headers) | ||
}) | ||
.then(res => res.text()) | ||
.then(txt => querystring.parse(txt)); | ||
return results; | ||
} | ||
async getAccessToken(options) { | ||
const requestData = { | ||
url: `${this.oauth}/access_token`, | ||
method: "POST" | ||
}; | ||
var parameters = { "oauth_verifier": options.verifier }; | ||
if (parameters) requestData.url += "?" + querystring.stringify(parameters); | ||
let headers = {}; | ||
headers = this.client.toHeader( | ||
this.client.authorize(requestData, { | ||
key: options.key, | ||
secret: options.secret | ||
}) | ||
); | ||
const results = await Fetch(requestData.url, { | ||
method: "POST", | ||
headers: Object.assign({}, baseHeaders, headers) | ||
}) | ||
.then(res => res.text()) | ||
.then(txt => querystring.parse(txt)); | ||
return results; | ||
} | ||
async get(resource, parameters) { | ||
@@ -80,3 +134,3 @@ const requestData = { | ||
} | ||
async post(resource, body, parameters) { | ||
@@ -147,1 +201,2 @@ const requestData = { | ||
module.exports = Twitter; | ||
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
55169
245
189
6