Created
October 12, 2015 02:13
-
-
Save supersupermomonga/71ef8ac809c7ba70ed86 to your computer and use it in GitHub Desktop.
M2L32TH_TklmEvt57DzRR-sLo7O4tWOsV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
function getInstance(consumer_key, consumer_secret, scope) { | |
return new HatenaWebService_(consumer_key, consumer_secret, scope); | |
} | |
var HatenaWebService_ = function (consumer_key, consumer_secret, scope) { | |
this.consumer_key = consumer_key; | |
this.consumer_secret = consumer_secret; | |
this.scope = scope; | |
} | |
HatenaWebService_.prototype.getService = function() { | |
return OAuth1.createService('Hatena') | |
.setAccessTokenUrl('https://www.hatena.com/oauth/token') | |
.setRequestTokenUrl('https://www.hatena.com/oauth/initiate?scope='+this.scope) | |
.setAuthorizationUrl('https://www.hatena.ne.jp/oauth/authorize') | |
.setConsumerKey(this.consumer_key) | |
.setConsumerSecret(this.consumer_secret) | |
.setCallbackFunction('authCallback') | |
.setPropertyStore(PropertiesService.getUserProperties()) | |
} | |
HatenaWebService_.prototype.authorize = function() { | |
var service = this.getService(); | |
if (service.hasAccess()) { | |
Logger.log('Already authorized'); | |
} else { | |
var authorizationUrl = service.authorize(); | |
Logger.log('Open the following URL and re-run the script: %s', authorizationUrl); | |
} | |
} | |
HatenaWebService_.prototype.reset = function() { | |
var service = this.getService(); | |
service.reset(); | |
} | |
HatenaWebService_.prototype.authCallback = function(request) { | |
var service = this.getService(); | |
var isAuthorized = service.handleCallback(request); | |
var mimeType = ContentService.MimeType.TEXT; | |
if (isAuthorized) { | |
return ContentService.createTextOutput('Success').setMimeType(mimeType); | |
} else { | |
return ContentService.createTextOutput('Denied').setMimeType(mimeType); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment