首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >新的LinkedIn权限问题(Rails omniauth-linkedin gem)

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

Stack Overflow用户
提问于 2012-08-28 02:06:35
回答 1查看 2.8K关注 0票数 4

我将omniauth-linkedin gem用于我的rails应用程序:

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

它应该考虑到新的LinkedIn权限请求程序和范围,但我没有成功获得电子邮件地址或完整的配置文件(除默认配置文件概述以外的任何内容)。

这是我的omniauth.rb文件:

代码语言:javascript
运行
复制
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文件中,但没有成功(下面的电子邮件字段请求):

代码语言:javascript
运行
复制
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的某种特殊许可吗?感谢您的帮助!谢谢。

另外,下面是我的auth控制器的相关代码:

代码语言:javascript
运行
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-08 23:07:44

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

我只是使用在request_token_path中传递的作用域添加了配置

代码语言:javascript
运行
复制
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 )

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

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12147151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档