在Rails应用中获取Google Plus联系人 [英] Fetch Google plus contacts in Rails app

查看:50
本文介绍了在Rails应用中获取Google Plus联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails应用程序中实现了google登录功能,现在的情况是在我的应用程序中获取google plus联系人.

有一个Google Plus宝石可以使用,但是我不确定是否可以使用该宝石来满足我的要求.

什么是最好的解决方案.

关于, 卡兰(Karan)

解决方案

我遇到过同样的情况,没有一个明确的答案. 您正在使用什么宝石进行Google授权?如果您使用的是omniauth-google-oauth2,请使用以下解决方案:

我找到了一个人的帖子,该人遇到了您所要求的部分应用程序完成后遇到的其他问题,您可以在此处找到它-

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

解决方案

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

这篇关于在Rails应用中获取Google Plus联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆