Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've searched around a lot and seen various answers. Some seem like they may be outdated, and others use gems (omnicontacts) that I can't seem to get working with devise.

In my situation, I already get an oauth token from Google successfully. Now I'm trying to send a get request to the url given by Google to get all my contacts. I'm able to do this in Google's oauth2 playground, but not from my rails app.

Here is the relevant code:

require 'net/http'
require 'json'

class User < ActiveRecord::Base

 def get_google_contacts(auth_token)
  uri = URI.parse("https://www.google.com/m8/feeds/contacts/default/full?oauth_token=#{auth_token}&max-results=50000&alt=json")

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE # You should use VERIFY_PEER in production
  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)
  contacts = JSON.parse(response.body)

  puts contacts
 end
end

Notebooks Controller: this is where I want a user to be able to access their contacts from Google.

def show
 @contacts = current_user.get_google_contacts(current_user.oauth_token)
end

Here is the error log I get in my local:

JSON::ParserError in NotebooksController#show

746: unexpected token at '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Error
401
(Client Error)!!1</title>

<p><b>401.</b>
<ins>That&#39;s an error.</ins></p>
<p>There was an error in your request.
<ins>That&#39;s all we know.</ins></p></body></html>'

I think I may be using incompatible techniques to parse the xml I get from Google into json, but that is basically what I want to do. The only Rails related documentation I found in Google API documentation was dated 2009, but it mentioned the gdata gem. Should I be using that?

Thank you so much for any help on this.

EDIT

This is the response I get from Google Oauth2 Playground on a 200 ok (https://developers.google.com/oauthplayground/):

Content-type: application/atom+xml; charset=UTF-8
-content-encoding: gzip
<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom'
xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'
xmlns:gContact='http://schemas.google.com/contact/2008'
xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005'>
<id>[email protected]</id><updated>2014-01-13T18:34:22.842Z</updated><category
scheme='http://schemas.google.com/g/2005#kind'    term='http://schemas.google.com/contact/2008#contact'/>
<title type='text'>Kaustubh Bhardwaj's Contacts</title>

EDIT #2

Ok, I've isolated my problem. I'm getting Net::HTTPUnauthorized on my get request. Here is the updated code for the get request I'm making to Google Contacts API:

uri = URI.parse("https://www.google.com/m8/feeds/contacts/default/full?max-results=50000")
# headers = { "access_token" => "#{auth_token}" }

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE 
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header({ "access_token" => "#{auth_token}" })

response = http.request(request)
puts response.code
puts response

That is the same url I am using in Google Oauth2 playground.

share|improve this question
add comment

1 Answer

I suspect you're setting the token incorrectly. It is more normal to set it in an http header

Authorization: Bearer 0b79bab50daca910b000d4f1a2b675d604257e42

If you set it in the URL, I believe it's access_token=, rather than your oauth_token= but since I never use it this way, I might be wrong.

share|improve this answer
    
Thanks for your help, I'll try to set a header and see how it goes. Also, am I correctly converting the xml from Google to json? Because it doesn't seem to be giving me json data like I expect? Should I just give up on getting json data and figure out how to parse the xml? I get the feeling this is part of my problem as well. –  kaustubhb Jan 11 at 18:05
    
What XML? The response you got is not XML, it's HTML and is meant to be a human displayable error message. You will only receive JSON for a successful 200 response –  pinoyyid Jan 13 at 6:05
    
I edited the question to show you the 200 response I get when I make the get request using Google's Oauth2 Playground (developers.google.com/oauthplayground). When I don't get a 200 response then the format is HTML. –  kaustubhb Jan 13 at 18:43
    
Since you have this working in the Oauth Playground, I suggest you simply compare the URL from OP with the URL sent by your app and see what is different. Feel free to paste the two URLs side-by-side in your question. –  pinoyyid Jan 14 at 7:07
    
Sorry for the late reply, and thanks for your patience. I edited the question again with my Net:HTTP get request that's giving me an 'unauthorized' error. –  kaustubhb Jan 15 at 16:31
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.