我使用這樣的珍寶:無法檢索的access_token,從設計Omniauth認證策略refresh_token,軌道4,5
gem "omniauth-yandex"
gem 'devise'
我的設置。
路線:
devise_for :users, :controllers => { :omniauth_callbacks => "callbacks" }
設計初始化:
config.omniauth :yandex, Rails.application.secrets.client_id , Rails.application.secrets.password, callback_url: "http://cb2bcdc4.ngrok.io/users/auth/yandex/callback"
用戶模型:
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:yandex]
def self.from_omniauth(auth)
where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
user.provider = auth.provider
user.uid = auth.uid
user.email = auth.info.email
user.password = Devise.friendly_token[0,20]
end
end
CallbackController:
class CallbacksController < Devise::OmniauthCallbacksController
def yandex
#empty
end
end
查看:
<%= link_to "Sign in with Yandex", user_yandex_omniauth_authorize_path, id: "sign_in" %>
當我點擊 「登錄與Yandex的」 我的應用程序會提示用戶權限,然後重定向到我的應用程序。用戶在我的數據庫中創建了這樣的字段 - 電子郵件,提供商,用戶體驗。但我想也有access_token,refresh_token和expires_at,因爲我使用的是很少的Yandex API。
當我httlog'ed上述動作(從點擊「登錄..」到重定向回我的應用程序),我收到了這些結果:
D, [2017-04-26T19:17:42.091838 #24865] DEBUG -- : [0;30;101m[httplog] Connecting: oauth.yandex.ru:443[0m
D, [2017-04-26T19:17:42.266645 #24865] DEBUG -- : [0;30;101m[httplog] Sending: POST http://oauth.yandex.ru:443/token[0m
D, [2017-04-26T19:17:42.267040 #24865] DEBUG -- : [0;30;101m[httplog] Data: client_id=097253682f9f41289ec5exxxxxxx&client_secret=xxxxxxdb4fxx0eadcbb8a4143&code=xxxxx327&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fcb2bcdc4.ngrok.io%2Fusers%2Fauth%2Fyandex%2Fcallback%3Fstate%xxxxxxxxxxx%26code%xxxx[0m
D, [2017-04-26T19:17:42.410712 #24865] DEBUG -- : [0;30;101m[httplog] Status: 200[0m
D, [2017-04-26T19:17:42.410945 #24865] DEBUG -- : [0;30;101m[httplog] Benchmark: 0.143445 seconds[0m
D, [2017-04-26T19:17:42.411168 #24865] DEBUG -- : [0;30;101m[httplog] Response:
{"token_type": "bearer", "access_token": "xxxxxxxxuyBwtcyAFjkBZo3F3MCiIaTI", "expires_in": 31528753, "refresh_token": "xxxxxxxxxxxClSH:Pts0u-Mfls-vdEc7-zTOod9ZWzegNFRxxxxxxxxxxxxxKHpwsqBFUHHKtg"}[0m
D, [2017-04-26T19:17:42.414748 #24865] DEBUG -- : [0;30;101m[httplog] Connecting: login.yandex.ru:443[0m
D, [2017-04-26T19:17:42.609376 #24865] DEBUG -- : [0;30;101m[httplog] Sending: GET http://login.yandex.ru:443/info?format=json[0m
D, [2017-04-26T19:17:42.609720 #24865] DEBUG -- : [0;30;101m[httplog] Data: [0m
D, [2017-04-26T19:17:42.675702 #24865] DEBUG -- : [0;30;101m[httplog] Status: 200[0m
D, [2017-04-26T19:17:42.675972 #24865] DEBUG -- : [0;30;101m[httplog] Benchmark: 0.065791 seconds[0m
D, [2017-04-26T19:17:42.676211 #24865] DEBUG -- : [0;30;101m[httplog] Response:
{"first_name": "xxxxxxxxxxx9", "last_name": "xxxxxxxxxxxxxxx", "display_name": "xxxxx", "emails": ["[email protected]"], "default_email": "[email protected]", "real_name": "xxxxxx2", "login": "xxxxxxx", "sex": "male", "id": "xxxx123"}[0m
問題:我如何保存ACCESS_TOKEN,將Omniauth身份驗證過程中的令牌刷新爲用戶,因爲它清楚可見(第7行),因此它可以使用我的client_id和祕密而不使用任何代碼來檢索它。
謝謝。
我曾嘗試:
新增user.access_token = auth.credentials.token
到self.from_omniauth(auth)
方法在用戶模型。但沒有任何積極的變化。
UPDATE:在不同的平臺存在
同樣的問題。例如,共享主機和Linux Ubuntu(從頭開始全新的項目)。
您是否嘗試在'CallbackController'的'yandex'動作中調試參數?像'puts params'?我很確定access_token在'params'中返回,而不在'auth'哈希中。 –
@Зелёный,是的,我試過了。我只得到'國家'和'代碼'的參數。 – Edgars
請分享您通過'p response.headers.fetch('Access-Token')'或'p response.headers.fetch('access_token')'或者'p response.headers' –