3

I'm trying to access all the contacts from Google Contacts for invite purposes from my site. So I'm using OAuth2 but when I first request I get a 401 error with access_token. :(

Keep in mind I'm working local!!

Here's my code.

 def authenticate
      # initiate authentication w/ gmail
      # create url with url-encoded params to initiate connection with contacts api
      # next - The URL of the page that Google should redirect the user to after authentication.
      # scope - Indicates that the application is requesting a token to access contacts feeds.
      # secure - Indicates whether the client is requesting a secure token.
      # session - Indicates whether the token returned can be exchanged for a multi-use (session) token.
      next_param = "http://localhost:3000/import/authorise" 
      scope_param = "https://www.google.com/m8/feeds/"  
      #session_param = "1"
      state="profile"
      client_id="535946964206.apps.googleusercontent.com"
      root_url = "https://accounts.google.com/o/oauth2/auth"
      response_type="token"
      #TO FIND MORE ABOUT THESE PARAMETERS AND THERE MEANING SEE GOOGLE API DOCS.
      query_string = "?scope=#{scope_param}&state=#{state}&redirect_uri=#{next_param}&response_type=#{response_type}&client_id=#{client_id}"

      #render :text=> root_url + query_string and return
    #  redirect_to "https://accounts.google.com/o/oauth2/auth?
    #scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile&
    #state=%2Fprofile&
    #redirect_uri=http://localhost:3000/import/authorise&
    #response_type=token&
    #client_id=535946964206.apps.googleusercontent.com
    #"
      redirect_to root_url + query_string
    end

this is for authentication now i get response with access token

http://localhost:3000/import/authorise#state=profile&access_token=ya29.1.AADtN_WPohnlKYBH16kFrqEcYUTZ558WDICpqCqiz1mwBKESFr5PTiJFA5vsyoc&token_type=Bearer&expires_in=3600

Now next it will redirected to here,this is where i get above error

# YOU WILL BE REDIRECTED TO THIS ACTION AFTER COMPLETION OF AUTHENTICATION PROCESS WITH TEMPORARY SINGLE USE AUTH TOKEN.
def authorise
  #FIRST SINGEL USE TOKEN WILL BE RECEIVED HERE..
  token = params[:token]


  #PREPAIRING FOR SECOND REQUEST WITH AUTH TOKEN IN HEADER.. WHICH WILL BE EXCHANED FOR PERMANENT AUTH TOKEN.
  uri = URI.parse("https://www.google.com")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  path = '/accounts/AuthSubSessionToken'
  headers = {'Authorization' => "AuthSub token=#{token}"}

  render :text=> http.get(path, headers) and return
  #GET REQUEST ON URI WITH SPECIFIED PATH...
  resp, data = http.get(path, headers)

  #ender :text=> data and return
  resp.code="200"
  #SPLIT OUT TOKEN FROM RESPONSE DATA.
  if resp.code == "200"
    token = ''
    data.split.each do |str|
    if not(str =~ /Token=/).nil?
      token = str.gsub(/Token=/, '')
    end
  end


return redirect_to(:action => 'import', :token => token)
else
redirect_to root_url , :notice => "fail"
end
end

here at above function when it requests resp, data = http.get(path, headers) the responce resp.code is 401 . i dont know what's wrong i'm following https://gist.github.com/miketucker/852849 example which uses AuthSub which is now not in use so i'm adapting oauth2 . i already have made app at clode console and given return url and other info.

any help or point me to other working example,thanks you!!! thank you.

prajmus
  • 3,171
  • 3
  • 31
  • 41
Akki209
  • 123
  • 12

0 Answers0