Fetch Google plus contacts in Rails app

好久不见. 提交于 2019-12-12 02:58:04

问题


I have google login feature implemented in my Rails app and now the scenario is to fetch google plus contacts in my app.

There is one Google Plus gem available but i am not sure whether i can fullfill my requirements using that gem.

Whats the best possible solution to this.

Regards, Karan


回答1:


You can use the Google APIs Ruby Client and do something like:

client = Google::APIClient.new
plus = client.discovered_api('plus')

# Code to authorize the client.
...

result = client.execute(plus.people.list,
  :collection => 'visible',
  :userId => 'me')

Where the code that you need to authorize the client depends on the flow that you are using to implement the login.




回答2:


I've encounter the same situation, nowhere was clear answer. What gem you are using for Google Authorization? If you are using omniauth-google-oauth2 here is the solution:

I've found post of a guy who encounter different problem AFTER part of application you are asking for was done, you may find it here - http://blog.baugues.com/google-calendar-api-oauth2-and-ruby-on-rails

Down to the code, this how you callback function in a controller (after logging in) should looks like:

 def create  #lets say it is session#new controller
omniauth = request.env["omniauth.auth"]
authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
initial_session(omniauth) unless current_user
 client = Google::APIClient.new()
 client.authorization.access_token = omniauth["credentials"]["token"]
 plus = client.discovered_api('plus')
 contacts = client.execute(plus.people.list, :collection => 'visible',
                                           :userId => 'me')
 raise contacts.inspect.to_s


来源:https://stackoverflow.com/questions/16778450/fetch-google-plus-contacts-in-rails-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!