問題描述
我已經在 php 中實現了 oAuth(目前用于 twitter),正如我在幾個教程中讀到的,你應該將訪問令牌存儲在 db 中以備將來使用.但是,我不知道您如何知道您是否為特定用戶存儲了訪問令牌,以決定是否應將其從數據庫中取出或重新生成.這是描述我的問題的流程:
I've implemented the oAuth in php (currently for twitter) and as I've read in several tutorials you should store the access token in db for future use. However I don't see how you know if you have the access token stored for a particular user to decide if you should pull it out of the db or regenerate it. Here's a flow describing my question:
- 獲取請求令牌
- 將用戶發送到提供商的身份驗證頁面
- 用戶使用 oauth 令牌和 oauth 驗證器返回回調 url
- 獲取訪問令牌
- 在數據庫上保存訪問令牌/user_id/screen_name 以備將來使用
- 如果用戶沒有注銷,訪問令牌仍然在服務器會話變量中.否則,重復過程.
- 獲取請求令牌
- 將用戶發送到提供商的身份驗證頁面
- 用戶使用 oauth 令牌和 oauth 驗證器返回回調 url
- (此時我只有 oauth 令牌,我如何知道用戶之前是否使用 twitter 登錄并從 db 中提取了他們的訪問令牌?)
- 如果是用戶第一次登錄,生成訪問令牌.
oAuth 的主要工作流程很明確,但不清楚如何處理回訪用戶以及應存儲或不存儲哪些數據.
The main workflow for oAuth is clear, however it is not clear how to handle returning users and which data should be stored or not.
一百萬謝謝!
推薦答案
您不應為每次訪問重新生成令牌.僅在過期時生成它.我已經使用 OAuth 構建了 twitter 應用程序.這是我的流程:
You should not regenerate token for each access. Generate it only when it's expired. I've build twitter application using OAuth. Here my flow:
當用戶登錄時,我會檢查他們在數據庫中是否有令牌
when user login, I will check if they have token in DB
1.1.如果不存在,則對其進行身份驗證,然后存儲和使用生成的令牌
1.1. If it's not exists, authenticate them and then store and use the resulting token
1.2.如果存在,請使用它.
1.2. If it's exists, use it.
1.2.1.如果 twitter 沒有抱怨,那么令牌仍然有效,使用它.
1.2.1. If twitter doesn't complain, then the token still valid, use it.
1.2.2.如果 twitter 抱怨,則令牌已過期.返回 1.1.
1.2.2. If twitter complained, then the token is expired. Return to 1.1.
1.2.3.如果在 x 重試之后 twitter 仍然抱怨.有問題,通知管理員!
1.2.3. If after x retry twitter still complained. Something wrong, notify admin!
這是圖形解釋:
這篇關于每次用戶登錄時都應該生成oAuth中的訪問令牌嗎?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!