問題描述
我一直在嘗試讓 Google 的日歷 API 在 PHP 網(wǎng)絡(luò)中運(yùn)行申請(qǐng),但我很難通過身份驗(yàn)證.
I've been trying to get Google's Calendar API working in a PHP web application, but I'm having a hard time getting authenticated.
我想要做的是允許用戶與服務(wù)器已知的單個(gè)帳戶的日歷進(jìn)行交互.
What I want to do is to allow users to interact with calendars of a single account known by the server.
OAuth 2.0 文檔 中涵蓋的每種類型的場景都談?wù)撚脩敉?這涉及登錄表單和個(gè)人用戶登錄,但我希望服務(wù)器本身直接進(jìn)行身份驗(yàn)證并為自己獲取訪問令牌.
Each type of scenario covered in the OAuth 2.0 docs talks about "user consent" which involves a login form and the individual user logging in, but I want the server itself to authenticate directly and obtain an access token for itself.
是否有 OAuth 的某些部分或我可以使用的替代機(jī)制來執(zhí)行此操作?
Is there some part of OAuth or some alternative mechanism I can use to do this?
推薦答案
為此,您必須完成用戶同意的步驟,然后將其提供給您的訪問令牌復(fù)制到 PHP 代碼中.
In order to do this, you must go through the steps for user consent and then copy the access tokens it gives you into the PHP code.
OAuth 通常的流程是這樣的:
The usual procedure for OAuth is like this:
- 將用戶發(fā)送到身份驗(yàn)證頁面.
- 用戶返回 $_GET['code']
- 將 $_GET['code'] 發(fā)送到 OAuth 服務(wù)器以獲取令牌
- 在數(shù)據(jù)庫中為用戶存儲(chǔ)令牌(或會(huì)話,如果它非常短暫)
但是當(dāng)使用這樣的單個(gè)日歷進(jìn)行操作時(shí),您需要修改步驟 4.相反,您將令牌轉(zhuǎn)儲(chǔ)到屏幕并將其作為變量復(fù)制到您的 PHP 文件中,而不是將其放入數(shù)據(jù)庫中.然后,當(dāng)您將訪問令牌傳遞給服務(wù)器時(shí),您只需傳遞已知的靜態(tài)令牌,而不是來自數(shù)據(jù)庫/會(huì)話的動(dòng)態(tài)令牌.
But when doing it with a single calendar like this, you modify step 4. Instead, you dump the token to screen and copy it into your PHP file as variables, instead of putting it in the database. Then when you go to pass the access token to the server, you just pass the known, static token rather than a dynamic token from the database / session.
這篇關(guān)于服務(wù)器的 Google API 身份驗(yàn)證的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!