【发布时间】:2016-06-11 18:01:58
【问题描述】:
我一直在使用 gem LinkedIn OAuth 2.0。现在我可以让它生成linkedin登录页面。但是,接下来应该发生的事情是它向我的回调链接发送一个代码,我用它来生成访问令牌。问题是变量 'oauth' 是在身份验证操作中生成的,但随后需要在回调操作中再次使用。我尝试使用完全相同的参数再次生成 oauth 变量,但是当我这样做时,我得到了 SSL 证书错误。似乎在这两种情况下都需要使用完全相同的 oauth 实例。如果您有任何想法,请告诉我。我的代码如下:
def authenticate
require "linkedin-oauth2"
LinkedIn.configure do |config|
config.client_id = "Mycode"
config.client_secret = "Mysecret"
# This must exactly match the redirect URI you set on your application's
# settings page. If your redirect_uri is dynamic, pass it into
# `auth_code_url` instead.
config.redirect_uri = "http://localhost:3000/auth/linkedin/callback"
end
oauth = LinkedIn::OAuth2.new()
url = oauth.auth_code_url
redirect_to url
end
def callback
require "linkedin-oauth2"
code = params[:code]
access_token = oauth.get_access_token(code)
api = LinkedIn::API.new(access_token)
my_job_titles = api.profile(fields: ["id", {"positions" => ["title"]}])
puts my_job_titles
redirect_to("/")
end
结束
【问题讨论】:
标签: ruby-on-rails oauth-2.0 linkedin-api