UNPKG

scribe-node

Version:

Scribe java OAuth library port to node.js

295 lines (237 loc) 12.9 kB
### Development cycle for updating scribe-node and hubot: - edit coffee files - cake build - increase version number to package.json - commit changes to github - npm publish - update version to hubot package.json - git add . git commit -m "" git push heroku master - test 0. make sure you have added environment api and secret for apis you try to use 1. get linkedin authorization url 2. Not real tests done yet, just use cases scetched from development process ### #grant_type=refresh_token ### Such a request will look similar to the following: POST /o/oauth2/token HTTP/1.1 Host: accounts.google.com Content-Type: application/x-www-form-urlencoded client_id=8819981768.apps.googleusercontent.com& client_secret={client_secret}& refresh_token=1/6BMfW9j53gdGImsiyUH5kU5RsR4zwI9lUVX-tqf8JXQ& grant_type=refresh_token As long as the user has not revoked the access granted to your application, the response includes a new access token. A response from such a request is shown below: { "access_token":"1/fFBGRNJru1FQd44AzqT3Zg", "expires_in":3920, "token_type":"Bearer" } ### ### verb = @api.getAccessTokenVerb() request = new OAuthRequest verb, @api.getAccessTokenEndpoint() # TODO: is this universal behavior or just Google requires to add params on body on post? if verb == Verb.POST || verb == Verb.PUT for key, value of params request.addBodyParameter key, value else for key, value of params request.addQueryStringParameter key, value #NOTE: at least Google requires special content type (application/x-www-form-urlencoded) on headers for key, value of @api.getHeaders() request.addHeader key, value return request.send cb ### https://code.google.com/apis/console/?pli=1#project:1041764243423:access {"error":{"errors":[{"domain":"global","reason":"authError","message":"Invalid Credentials","locationType":"header","location":"Authorization"}],"code":401,"message":"Invalid Credentials"}} Client ID: 1041764243423.apps.googleusercontent.com Client secret: 4jCbEOep-wjom-3rwh-hAU9G Redirect URIs: urn:ietf:wg:oauth:2.0:oob http://localhost https://www.googleapis.com/analytics/v3/management/accounts http://code.google.com/intl/fi-FI/apis/accounts/docs/OAuth2InstalledApp.html https://github.com/jashkenas/coffee-script/wiki/[HowTo]-Compiling-and-Setting-Up-Build-Tools Content-Type' is set to :'application/x-www-form-urlencoded FOR POSTS! #rawResponse: '{\n "access_token" : "ya29.AHES6ZQByLeXLljj_eQGBmCSGXOaW2wvmNcGLlo6S-1E",\n "token_type" : "Bearer",\n "expires_in" : 3600,\n "refresh_token" : "1/woefyvx_8SnLqyrWaY6dPw_AmJZSDiHDZac4P50BwXY"\n}', ### # sprintf function sprintf = (format, args...) -> i = 1 format.replace /%((%)|s)/g, (m) -> m[2] || args[i++] || args ### # Retrieve information about your site(s) via Google Analytics API # # Uses node-scribe oauth kit which provides permanent access token for external applications # # http://pkumar.github.com/10-23-2011/nodejs-module-in-coffee.html # jacobrask: # I currently (after advice from here) have an index.js file in my package which require's coffeescript and then my coffeescript files # I might switch to "preinstall" instead later # Retrieve information about your site(s) via Google Analytics API # # Uses node-scribe oauth kit which provides permanent access token for external applications # # http://pkumar.github.com/10-23-2011/nodejs-module-in-coffee.html # jacobrask: # I currently (after advice from here) have an index.js file in my package which require's coffeescript and then my coffeescript files # I might switch to "preinstall" instead later # see also: https://github.com/ciaranj/node-oauth/blob/master/lib/oauth.js # http://js2coffee.org/ # http://code.google.com/intl/fi-FI/apis/analytics/docs/gdata/gdataInteractiveSamples.html # http://code.google.com/apis/ajax/playground/#map_simple # # now use API. if API call fails with access, then new access token should be retrieved # also if access token fails on retrieval, then new request token with verification should be done #c = require 'scripts/analytics' #service = new c.ServiceBuilder().provider(c.GoogleApi).apiKey("your_api_key").apiSecret("your_api_secret").build() http://googlecodesamples.com/oauth_playground/ OAuth 1.0a FEEDS / SCOPES Analytics https://www.google.com/analytics/feeds/ Google Base https://www.google.com/base/feeds/ Book Search https://www.google.com/books/feeds/ Blogger https://www.blogger.com/feeds/ Calendar https://www.google.com/calendar/feeds/ Contacts https://www.google.com/m8/feeds/ Chrome Web Store https://www.googleapis.com/auth/chromewebstore.readonly Documents List https://docs.google.com/feeds/ Finance https://finance.google.com/finance/feeds/ GMail https://mail.google.com/mail/feed/atom Health https://www.google.com/health/feeds/ H9 https://www.google.com/h9/feeds/ Maps https://maps.google.com/maps/feeds/ Moderator https://www.googleapis.com/auth/moderator OpenSocial https://www-opensocial.googleusercontent.com/api/people/ Orkut REST https://www.googleapis.com/auth/orkut Orkut https://orkut.gmodules.com/social/rest Picasa Web https://picasaweb.google.com/data/ Sidewiki https://www.google.com/sidewiki/feeds/ Sites https://sites.google.com/feeds/ Spreadsheets https://spreadsheets.google.com/feeds/ Tasks https://www.googleapis.com/auth/tasks URL Shortener https://www.googleapis.com/auth/urlshortener Webmaster Tools https://www.google.com/webmasters/tools/feeds/ YouTube https://gdata.youtube.com HUBOT_GOOGLE_OAUTH_API_KEY HUBOT_GOOGLE_OAUTH_API_SECRET HUBOT_GOOGLE_OAUTH_SCOPE heroku config:add GOOGLE_OAUTH_API_KEY="www.articlemanager.us" --app articlemanager heroku config:add GOOGLE_OAUTH_API_SECRET="tD_FisW209w4ZRyE-ZoQwn8J" --app articlemanager heroku config:add GOOGLE_OAUTH2_API_KEY="1041764243423.apps.googleusercontent.com" --app articlemanager heroku config:add GOOGLE_OAUTH2_API_SECRET="4jCbEOep-wjom-3rwh-hAU9G" --app articlemanager heroku config:add TWITTER_OAUTH_API_KEY="oYrnYokARdvvdzxAH369g" --app articlemanager heroku config:add TWITTER_OAUTH_API_SECRET="HuTlXxdvJJTOedJKc6Q3r9BrwaocVZF4dzcFruKFiQ" --app articlemanager heroku config:add GITHUB_OAUTH2_API_KEY="db6b877a413419a253cb" --app articlemanager heroku config:add GITHUB_OAUTH2_API_SECRET="43c337e918f53ff5a4519332240ab8eda14f90a6" --app articlemanager heroku config:add FACEBOOK_OAUTH_API_KEY="142458315817023" FACEBOOK_OAUTH_API_SECRET="a4906262d3b86fc30c7e18ddac0a8302" --app articlemanager heroku config:add HUBOT_CAMPFIRE_TOKEN="678607930babfb7ccdb8f1af9253cf5237615667" HUBOT_CAMPFIRE_ROOMS="uranus,analytics" HUBOT_CAMPFIRE_ACCOUNT="uranus" heroku config:add LINKEDIN_OAUTH_API_KEY="0b8gftoy75lq" LINKEDIN_OAUTH_API_SECRET="fDwvrYWYRAiy6l2b" --app articlemanager OAuth 2.0 SCOPES Retrieved from: http://code.google.com/apis/explorer/ -> APIs: https://www.googleapis.com/discovery/v1/apis?fields=items%2FdiscoveryLink&pp=1 https://www.googleapis.com/discovery/v1/apis/adsense/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/analytics/v3/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/audit/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/blogger/v2/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/books/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/customsearch/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/discovery/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/freebase/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/groupssettings/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/latitude/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/moderator/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/orkut/v2/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/pagespeedonline/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/plus/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/prediction/v1.4/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/shopping/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/siteVerification/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/taskqueue/v1beta1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/tasks/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/translate/v2/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/urlshortener/v1/rest?fields=auth&pp=1 https://www.googleapis.com/discovery/v1/apis/webfonts/v1/rest?fields=auth&pp=1 And scopes are: https://www.googleapis.com/auth/adsense https://www.googleapis.com/auth/adsense.readonly https://www.googleapis.com/auth/analytics.readonly https://www.googleapis.com/auth/blogger https://www.googleapis.com/auth/books https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/calendar.readonly https://www.googleapis.com/auth/apps.groups.settings https://www.googleapis.com/auth/latitude.all.best https://www.googleapis.com/auth/latitude.all.city https://www.googleapis.com/auth/latitude.current.best https://www.googleapis.com/auth/latitude.current.city https://www.googleapis.com/auth/moderator https://www.googleapis.com/auth/orkut https://www.googleapis.com/auth/orkut.readonly https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/prediction https://www.googleapis.com/auth/siteverification https://www.googleapis.com/auth/taskqueue https://www.googleapis.com/auth/taskqueue.consumer https://www.googleapis.com/auth/tasks https://www.googleapis.com/auth/tasks.readonly https://www.googleapis.com/auth/urlshortener https://www.googleapis.com/auth/userinfo https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email google gdata related node module: "gdata": "0.1.0" # three operations to do before using any service c.get_authorization_url service c.set_verification_code code c.set_access_token service # main access token retrieval method after oauth dance access_token = c.get_access_token() # and finally retrieving data from API analytics_accounts_feed = 'https://www.google.com/analytics/feeds/accounts/default' analytics_accounts_feed = 'https://www.google.com/analytics/feeds/accounts/default?max-results=50&v=0.3' user_calendars_feed = 'http://www.google.com/calendar/feeds/default/allcalendars/full?orderby=starttime' chart_feed = 'https://chart.googleapis.com/chart?chid='+get_nonce() handle_analytics_feed = (extractor, response) -> console.log 'response: '+response handle_calendar_feed = (extractor, response) -> console.log 'response: '+response service.signedRequest(access_token, handle_analytics_feed, analytics_accounts_feed) service.signedRequest(access_token, handle_calendar_feed, user_calendars_feed) c = require 'scripts/analytics' service = new c.ServiceBuilder().provider(c.GoogleApi).apiKey('www.articlemanager.us').apiSecret('tD_FisW209w4ZRyE-ZoQwn8J')._scope('https://www.google.com/analytics/feeds/').build() access_token = c.get_access_token() analytics_accounts_feed = 'https://www.google.com/analytics/feeds/accounts/default' analytics_accounts_feed = 'https://www.google.com/analytics/feeds/accounts/default?max-results=5' analytics_accounts_feed = 'https://www.google.com/analytics/feeds/data?start-date=2011-01-11&end-date=2010-01-11&dimensions=ga:date&metrics=ga:visits,ga:pageviews&sort=ga:date&ids=ga:4463875'; analytics_accounts_feed = 'https://www.google.com/analytics/feeds/data?start-date=2011-01-11&end-date=2011-01-11&dimensions=ga:date&metrics=ga:visits,ga:pageviews&&sort=ga:date&ids=ga:4463875' chart_feed = 'https://chart.googleapis.com/chart?chid='+c.get_nonce() handle_analytics_feed = (extractor, response) -> console.log 'response: '+response service.signedRequest(access_token, handle_analytics_feed, analytics_accounts_feed) base64 = require 'scripts/base64' c = require 'scripts/analytics' service = new c.ServiceBuilder().provider(c.GoogleApi).apiKey('www.articlemanager.us').apiSecret('tD_FisW209w4ZRyE-ZoQwn8J')._scope('https://www.google.com/analytics/feeds/').build() access_token = c.get_access_token() chart_feed = 'https://chart.googleapis.com/chart?chid='+c.get_nonce() handle_chart_feed = (extractor, response) -> console.log '<img src="data:image/gif;base64,'+new Buffer(response, 'binary').toString('base64')+'" />' chd = 't:' i = 0 while i < 150\ chd = chd + (Math.floor Math.random()*100000) + ','\ i++ chds = chd.substr 0, chd.length-1 params = {\ cht: 'lc'\ chs: '600x200'\ chds: '0,100000'\ chd: chds\ chof: 'gif'\ } service.signedImagePostRequest(access_token, handle_chart_feed, chart_feed, params) ###