Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. require 'rubygems'
  2. require 'google/api_client'
  3. require 'launchy'
  4.  
  5. CLIENT_ID = 'my_app_Id_on_gmail_developers_console'
  6. CLIENT_SECRET = 'the_secret_key'
  7. OAUTH_SCOPE = 'https://mail.google.com/'
  8. REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob'
  9.  
  10. # Create a new API client & load the Google Drive API
  11. client = Google::APIClient.new(:application_name => 'Ruby Gmail sample',
  12. :application_version => '1.0.0')
  13. gmail = client.discovered_api('gmail', "v1")
  14.  
  15. # Request authorization
  16. client.authorization.client_id = CLIENT_ID
  17. client.authorization.client_secret = CLIENT_SECRET
  18. client.authorization.scope = OAUTH_SCOPE
  19. client.authorization.redirect_uri = REDIRECT_URI
  20.  
  21. uri = client.authorization.authorization_uri
  22. Launchy.open(uri)
  23.  
  24. # Exchange authorization code for access token
  25. $stdout.write "Enter authorization code: "
  26. client.authorization.code = gets.chomp
  27. client.authorization.fetch_access_token!
  28.  
  29. #testing if it is working well by counting the emails.
  30. @emails = client.execute(
  31. api_method: gmail.users.messages.list,
  32. parameters: {
  33. userId: "me"},
  34. headers: {'Content-Type' => 'application/json'}
  35. )
  36.  
  37. count = @emails.data.messages.count
  38. puts "you have #{count} emails "
  39. # Pretty print the API result
  40. jj @emails.data.messages
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement