Skip to content

Instantly share code, notes, and snippets.

@decors
Last active January 15, 2016 03:52
Show Gist options
  • Save decors/51a897eef68802f0f111 to your computer and use it in GitHub Desktop.
Save decors/51a897eef68802f0f111 to your computer and use it in GitHub Desktop.
Electron twitter OAuth
'use strict';
var remote = require('remote');
var BrowserWindow = remote.require('browser-window');
var Twitter = require('twitter');
var OAuth = require('oauth').OAuth;
var authUrl = "https://api.twitter.com/oauth/authenticate?oauth_token=";
var oauth = new OAuth(
'https://api.twitter.com/oauth/request_token',
'https://api.twitter.com/oauth/access_token',
'key',
'secret',
'1.0A',
null,
'HMAC-SHA1'
);
oauth.getOAuthRequestToken(function(error, oauth_token, oauth_token_secret, results) {
if(error) {
console.error(error);
return;
}
console.log('oauth_token :' + oauth_token);
console.log('oauth_token_secret :' + oauth_token_secret);
var url = authUrl + oauth_token;
var loginWindow = new BrowserWindow({width: 800, height: 600});
loginWindow.webContents.on('will-navigate', function (event, url) {
var urlinfo = require('url').parse(url, true);
if(urlinfo.query.oauth_verifier) {
oauth.getOAuthAccessToken(oauth_token, oauth_token_secret, urlinfo.query.oauth_verifier, function(error, oauth_access_token, oauth_access_token_secret) {
if(error) {
console.error(error);
return;
}
console.log('oauth_access_token : ' + oauth_access_token);
console.log('oauth_access_token_secret : ' + oauth_access_token_secret);
loginWindow.close();
});
} else {
loginWindow.close();
}
event.preventDefault();
});
loginWindow.loadUrl(url);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment