• 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

修订版d74b2b5343f1a9212468eeb6ca0d285eefd54e20 (tree)
时间2016-02-11 00:23:15
作者Hiromichi Matsushima <hylom@Hiro...>
CommiterHiromichi Matsushima

Log Message

implement .sprnkr setting file to store tokens

更改概述

差异

--- a/main.js
+++ b/main.js
@@ -40,9 +40,14 @@ app.on('ready', function() {
4040 });
4141 });
4242
43- client.getCallbackUrl(function(err, authUrl) {
44- createWindow(authUrl);
45- });
43+ if (client.loadTokens()) {
44+ app._clients = [client];
45+ createWindow();
46+ } else {
47+ client.getCallbackUrl(function(err, authUrl) {
48+ createWindow(authUrl);
49+ });
50+ }
4651 });
4752
4853 app.on('window-all-closed', function() {
--- a/model/models.js
+++ b/model/models.js
@@ -2,9 +2,36 @@
22
33 var OAuth = require('oauth');
44 var Twitter = require('twitter');
5-
5+var path = require('path');
6+var fs = require('fs');
67 var Client = function () {};
78
9+var CONFIG_FILE = path.join(process.env.HOME, '.sprnkr');
10+
11+Client.prototype.loadTokens = function loadTokens () {
12+ var jsonData;
13+ try {
14+ jsonData = fs.readFileSync(CONFIG_FILE, 'utf8');
15+ } catch (e) {
16+ return false;
17+ }
18+ var data = JSON.parse(jsonData);
19+ this.accessToken = data.tokens.access_token;
20+ this.accessTokenSecret = data.tokens.access_token_secret;
21+ this.createTwitterClient();
22+ return true;
23+};
24+
25+Client.prototype.saveTokens = function saveTokens () {
26+ var jsonData = JSON.stringify({
27+ tokens: {
28+ access_token: this.accessToken,
29+ access_token_secret: this.accessTokenSecret
30+ }
31+ });
32+ fs.writeFileSync(CONFIG_FILE, jsonData, 'utf8');
33+};
34+
835 Client.prototype.getCallbackUrl = function getCallbackUrl (callback) {
936 var client = this;
1037 function sendCallback(err, token, secret, results) {
@@ -17,18 +44,25 @@ Client.prototype.getCallbackUrl = function getCallbackUrl (callback) {
1744 this.oauth.getOAuthRequestToken(sendCallback);
1845 };
1946
47+Client.prototype.createTwitterClient = function createTwitterClient() {
48+ this.twitter = new Twitter({
49+ consumer_key: this.consumerKey,
50+ consumer_secret: this.consumerSecret,
51+ access_token_key: this.accessToken,
52+ access_token_secret: this.accessTokenSecret
53+ });
54+}
55+
2056 Client.prototype.authorize = function authorize(token, verifier, callback) {
2157 var client = this;
2258 function createTwitterClient(err, token, secret, results) {
2359 if (err) {
2460 callback(err);
2561 }
26- client.twitter = new Twitter({
27- consumer_key: client.consumerKey,
28- consumer_secret: client.consumerSecret,
29- access_token_key: token,
30- access_token_secret: secret
31- });
62+ client.accessToken = token;
63+ client.accessTokenSecret = secret;
64+ client.saveTokens();
65+ client.createTwitterClient();
3266 callback(false);
3367 };
3468 client.oauth.getOAuthAccessToken(token, client.requestSecret, verifier, createTwitterClient);