• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javaandroidc++linuxc#objective-ccocoa誰得qtrubypythongamewindowsbathyscaphephpguic翻訳omegattwitterframeworktestbtronarduinovb.net計画中(planning stage)directxpreviewerゲームエンジンdom

electron based twitter client


Commit MetaInfo

修订版6725f35ee7db06b7b259db92e637e94698fc6b02 (tree)
时间2016-02-10 03:49:57
作者Hiromichi Matsushima <hylom@Hiro...>
CommiterHiromichi Matsushima

Log Message

implement oauth authentification

更改概述

差异

--- /dev/null
+++ b/auth.js
@@ -0,0 +1,34 @@
1+var OAuth = require('oauth');
2+var authInfo = require('./.account.json');
3+
4+var oauthToken = '';
5+var oauthSecret = '';
6+
7+var oauth = new OAuth.OAuth(
8+ 'https://api.twitter.com/oauth/request_token',
9+ 'https://api.twitter.com/oauth/access_token',
10+ authInfo.consumer_key,
11+ authInfo.consumer_secret,
12+ '1.0A',
13+ 'sprnkr://token',
14+ 'HMAC-SHA1'
15+);
16+
17+exports.callbackUrl = function callbackUrl (callback) {
18+
19+ function sendCallback(err, token, secret, results) {
20+ oauthToken = token;
21+ oauthSecret = secret;
22+
23+ var authUrl = 'https://twitter.com/oauth/authenticate?oauth_token=' + token;
24+ callback(err, authUrl);
25+ };
26+
27+ oauth.getOAuthRequestToken(sendCallback);
28+}
29+
30+exports.getTokens = function getTokens (token, verifier, callback) {
31+ console.log('token: ' + token);
32+ console.log('verifier: ' + verifier);
33+ oauth.getOAuthAccessToken(token, oauthSecret, verifier, callback);
34+};
--- a/jquery.min.js
+++ /dev/null
@@ -1 +0,0 @@
1-node_modules/jquery/dist/jquery.min.js
\ No newline at end of file
--- a/main.js
+++ b/main.js
@@ -1,19 +1,47 @@
11
2+var electron = require('electron');
23 var app = require('app');
34 var BrowserWindow = require('browser-window');
4-
5+var auth = require('./auth');
6+var url = require('url');
57 var mainWindow = null;
8+var models = require('./models');
69
7-function createWindow () {
10+function createWindow (url) {
811 mainWindow = new BrowserWindow({width: 400, height: 800});
9- mainWindow.loadURL('file://' + __dirname + '/index.html');
12+ if (url) {
13+ mainWindow.loadURL(url);
14+ } else {
15+ mainWindow.loadURL('file://' + __dirname + '/view/index.html');
16+ }
1017
1118 mainWindow.on('closed', function() {
1219 mainWindow = null;
1320 });
1421 }
1522
16-app.on('ready', createWindow);
23+app.on('ready', function() {
24+ var protocol = electron.protocol;
25+ protocol.registerFileProtocol('sprnkr', function (req, callback) {
26+ // req.url is 'sprnkr://token?oauth_token=***&oauth_verifier=***'
27+ var u = url.parse(req.url, true);
28+ //console.log('token: ' + u.query.oauth_token);
29+
30+ auth.getTokens(u.query.oauth_token, u.query.oauth_verifier,
31+ function (err, token, secret, results) {
32+ if (err) throw err;
33+ console.log('access token: ', token);
34+ console.log('access secret: ', secret);
35+ models.setTokens(token, secret);
36+ createWindow();
37+ });
38+ });
39+
40+ //createWindow();
41+ auth.callbackUrl( function (err, url) {
42+ createWindow(url);
43+ });
44+});
1745
1846 app.on('window-all-closed', function() {
1947 if (process.platform != 'darwin')
--- a/models.js
+++ b/models.js
@@ -4,6 +4,17 @@ var Twitter = require('twitter');
44 // OAuth parameters
55 var authInfo = require('./.account.json');
66 var client = new Twitter(authInfo);
7+var myAuth = {};
8+myAuth.consumer_key = authInfo.consumer_key;
9+myAuth.consumer_secret = authInfo.consumer_secret;
10+myAuth.access_token_key = authInfo.acess_token_key;
11+myAuth.access_token_secret = authInfo.access_token_secret;
12+
13+exports.setTokens = function (token, secret) {
14+ myAuth.access_token_key = token;
15+ myAuth.access_token_secret = secret;
16+ client = new Twitter(myAuth);
17+};
718
819 exports.getHomeTimeline = function (callback) {
920 client.get('statuses/home_timeline', callback);
--- a/package.json
+++ b/package.json
@@ -25,6 +25,7 @@
2525 "twitter": "1.2.5",
2626 "ejs": "2.4.1",
2727 "moment": "2.11.2",
28- "font-awesome": "4.5.0"
28+ "font-awesome": "4.5.0",
29+ "oauth": "0.9.14"
2930 }
3031 }
--- a/index.html
+++ b/view/index.html
@@ -4,7 +4,7 @@
44 <meta charset="UTF-8">
55 <title>Sprnkr</title>
66 <link type="text/css" rel="stylesheet" href="./main.css" />
7- <link rel="stylesheet" href="./node_modules/font-awesome/css/font-awesome.min.css" />
7+ <link rel="stylesheet" href="../node_modules/font-awesome/css/font-awesome.min.css" />
88 </head>
99 <body>
1010 <div id="page-header">