我正在用 Ruby 制作一个简单的应用程序,我需要获取用户的电子邮件。我正在使用 Ruby 的 Google API 客户端。我无法获取用户的电子邮件地址,我已经研究这个主题 2 天了,并且在用户登录后无法获取用户的电子邮件。我不想使用 Google+ 登录按钮来做到这一点,我希望以编程方式进行。我获得了谷歌的授权,但不知道如何检索登录用户的用户信息。我也尝试过使用 client = Google::APIClient.new
但我无法做到。下面是我的代码。我将感谢您的帮助。
scopes = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.readonly', 'https://www.googleapis.com/auth/plus.login','https://www.googleapis.com/auth/userinfo.email']
client_secrets = Google::APIClient::ClientSecrets.load(SECRETS_PATH)
auth_client = client_secrets.to_authorization
auth_client.update!(
:additional_parameters => {"access_type" => "offline"}
)
auth_client.update!(
:scope => scopes,
:redirect_uri => 'http://localhost:3000/google_oauth2')
最佳答案
嘿,你可以引用 gem Google Plus
我已经实现了相同的功能,并且从这些方面来看它运行良好。
集成以下 Gem 的步骤-
首先通过将 google_plus gem 包含在 GemFile 中来安装它。
从 Google Plus + 的 Google Console 生成 API key
像它一样使用之前包含它
GooglePlus.api_key = 'your key'
通过 Google ID 搜索任何人
person = GooglePlus::Person.get(123)
您将获得特定用户的完整用户信息
关于ruby-on-rails - 无法从 Ruby 中的 google api 获取用户信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540646/