新的LinkedIn权限问题(Rails-omniauth-LinkedIn-gem)



我正在为我的rails应用程序使用omniauth-linkedin-gem:

https://github.com/skorks/omniauth-linkedin

它应该考虑到新的领英权限申请程序和范围,但我没有成功获得电子邮件地址或完整的个人资料(除了默认的个人资料概述之外的任何东西)。

这是我的omniauth.rb文件:

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :linkedin, ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'], :scope => 'r_fullprofile r_emailaddress'
end

在用户输入她/他的凭据之前,完整的个人资料和电子邮件地址请求最初会发送到LinkedIn。但是,一旦用户登录,作用域请求就会丢失,并且只能访问默认的配置文件概述信息。

我认为我的问题与必须特别请求我想要的范围内的字段有关,正如gem页面上提到的那样(https://github.com/skorks/omniauth-linkedin)。尽管如此,我还是尝试将特定的字段请求添加到omniauth.rb文件中,但没有成功(下面的电子邮件字段请求):

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :linkedin, ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'], :scope => 'r_fullprofile+r_emailaddress',
  :fields => ["id", "email-address", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url", "location", "positions", "educations"]
end

显然,除了电子邮件地址、职位和教育等非默认字段外,我还应该请求配置文件概述的默认字段,以便访问后一个非默认字段。

LinkedIn的电子邮件地址应该很简单,因为它不像职位或教育那样是一个结构化的对象,我相信我缺少了我想要的职位和教育中特定领域的代码(不确定我会如何写,我也很乐意在这方面提供一些意见)。我正在使用我的新API密钥,所以我不确定问题出在哪里。我需要LinkedIn的某种特殊许可吗?感谢您的帮助!非常感谢。

此外,这是我的身份验证控制器的相关代码:

require 'linkedin'
class AuthController < ApplicationController
  def auth
    client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
    request_token = client.request_token(:oauth_callback => 
                                      "http://#{request.host_with_port}/callback")
    session[:rtoken] = request_token.token
    session[:rsecret] = request_token.secret
    redirect_to client.request_token.authorize_url
  end
  def callback
    client = LinkedIn::Client.new(ENV['LINKEDIN_KEY'], ENV['LINKEDIN_SECRET'])
    if current_user.atoken.nil? && current_user.asecret.nil?
      pin = params[:oauth_verifier]
      atoken, asecret = client.authorize_from_request(session[:rtoken], session[:rsecret], pin)
      current_user.atoken = atoken
      current_user.asecret = asecret
      current_user.uid = client.profile(:fields => ["id"]).id
      flash.now[:success] = 'Signed in with LinkedIn.'
    elsif current_user.atoken && current_user.asecret 
      client.authorize_from_access(current_user.atoken, current_user.asecret)
      flash.now[:success] = 'Signed in with LinkedIn.'
    end

不确定这是否是最优雅的解决方案,但这对我有效

我刚刚添加了带有request_token_path 中传递的作用域的配置

LINKEDIN_CONFIGURATION = { :site => 'https://api.linkedin.com',
:authorize_path => '/uas/oauth/authenticate',
:request_token_path =>'/uas/oauth/requestToken?scope=r_basicprofile+r_emailaddress+r_network+r_contactinfo',
:access_token_path => '/uas/oauth/accessToken' }
client = LinkedIn::Client.new(LINKEDIN_API_KEY, LINKEDIN_SECRET_KEY, LINKEDIN_CONFIGURATION )

代码的其余部分是相同的。

请确保在回调方法中也更改客户端构造函数。

最新更新