Permalink
Browse files

fix rake oauth2:token task; change it to require user to set their oa…

…uth2 id and secret
  • Loading branch information...
1 parent 7775e6b commit 03753bccf405c3507d0521d1099b4b8f3ddc7511 @tpitale committed Jan 3, 2013
Showing with 60 additions and 38 deletions.
  1. +60 −38 Rakefile
View
@@ -9,65 +9,87 @@ require 'oauth2'
# curl -d "client_id={{client_id}}&client_secret={{client_secret}}&code=4/8O1xUKWzOdJESG7ednnulZPsbyNt&grant_type=http://oauth.net/grant_type/device/1.0" https://accounts.google.com/o/oauth2/token
# { "access_token" : "ERspXATXoblahblahblah", "token_type" : "Bearer", "expires_in" : 3600, "refresh_token" : "1/balhaajsdfklasfdjs;df" }
-namespace :oauth2 do
- def client
+module OAuth2Helpers
+ def build_client(id, secret)
# This is my test client account for Legato.
- OAuth2::Client.new('779170787975.apps.googleusercontent.com', 'mbCISoZiSwyVQIDEbLj4EeEc', {
+ opts = {
:authorize_url => 'https://accounts.google.com/o/oauth2/auth',
:token_url => 'https://accounts.google.com/o/oauth2/token'
- })
+ }
+
+ OAuth2::Client.new(id, secret, opts)
end
- def auth_url
+ def auth_url(client)
client.auth_code.authorize_url({
:scope => 'https://www.googleapis.com/auth/analytics.readonly',
:redirect_uri => 'http://localhost'
})
end
+ extend self
+end
+
+namespace :oauth2 do
desc "Get a new OAuth2 Token"
task :token do
- `open "#{auth_url}"`
+ puts 'Get your OAuth2 id and secret from the API Console: https://code.google.com/apis/console#access'
+
+ puts
+ print 'Your OAuth2 id: '
+ oauth_id = $stdin.gets.strip
+ print 'Your OAuth2 secret: '
+ oauth_secret = $stdin.gets.strip
+
+ client = OAuth2Helpers.build_client(oauth_id, oauth_secret)
+
+ puts
+ puts "Opening the OAuth2 auth url: #{OAuth2Helpers.auth_url(client)} ..."
+ `open "#{OAuth2Helpers.auth_url(client)}"`
- print 'OAuth2 Code: '
- code = $stdin.gets
+ puts
+ print 'OAuth2 Code (in the url): '
+ code = $stdin.gets.strip
- access_token = client.auth_code.get_token(code.strip, :redirect_uri => 'http://localhost')
+ access_token = client.auth_code.get_token(code, :redirect_uri => 'http://localhost')
+
+ puts
+ puts "Here's your access token: "
puts access_token.token
end
end
-namespace :oauth do
- def consumer
- OAuth::Consumer.new('779170787975.apps.googleusercontent.com', 'mbCISoZiSwyVQIDEbLj4EeEc', {
- :site => "https://www.google.com",
- :request_token_path => "/accounts/OAuthGetRequestToken",
- :access_token_path => "/accounts/OAuthGetAccessToken",
- :authorize_path => "/accounts/OAuthAuthorizeToken"
- })
- end
+# namespace :oauth do
+# def consumer(id, secret)
+# OAuth::Consumer.new('779170787975.apps.googleusercontent.com', 'mbCISoZiSwyVQIDEbLj4EeEc', {
+# :site => "https://www.google.com",
+# :request_token_path => "/accounts/OAuthGetRequestToken",
+# :access_token_path => "/accounts/OAuthGetAccessToken",
+# :authorize_path => "/accounts/OAuthAuthorizeToken"
+# })
+# end
- def request_token
- @request_token ||= consumer.get_request_token({}, {:scope => "https://www.googleapis.com/auth/analytics.readonly"})
- end
+# def request_token
+# @request_token ||= consumer.get_request_token({}, {:scope => "https://www.googleapis.com/auth/analytics.readonly"})
+# end
- def auth_url
- request_token.authorize_url
- end
+# def auth_url
+# request_token.authorize_url
+# end
- def access_token
- @access_token ||= begin
- print 'OAuth Code: '
- code = $stdin.gets.strip
- request_token.get_access_token(:oauth_verifier => code)
- end
- end
+# def access_token
+# @access_token ||= begin
+# print 'OAuth Code: '
+# code = $stdin.gets.strip
+# request_token.get_access_token(:oauth_verifier => code)
+# end
+# end
- desc "Get a new OAuth Token"
- task :token do
- `open "#{auth_url}"`
+# desc "Get a new OAuth Token"
+# task :token do
+# `open "#{auth_url}"`
- puts "Token: #{access_token.token}"
- puts "Secret: #{access_token.secret}"
- end
-end
+# puts "Token: #{access_token.token}"
+# puts "Secret: #{access_token.secret}"
+# end
+# end

0 comments on commit 03753bc

Please sign in to comment.