我想使用 Gmail 提供的公司電子郵件發送電子郵件。為了做到這一點,我想將 Gmail API 與 rest 命令一起使用(基本上使用 php 程式代碼啟動,用于遺留目的)。
我有那個代碼:
我去這個網址:
// https://accounts.google.com/o/oauth2/auth?client_id=my_client_id&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://www.googleapis.com/auth/gmail.send&response_type=code
// and obtain a token like that : 4/1AX4XfWgmW0ZdxXpJn8YzkVeDs3oXZUHyJcR7abE2TuqQrcmo4c1W02ALD4I
/*
echo GoogleAuthCurl("GET", '', array(
'client_id' => $GOOGLE_CLIENT_ID,
'redirect_uri'=>'urn:ietf:wg:oauth:2.0:oob',
'scope' => 'https://www.googleapis.com/auth/gmail.send',
'response_type' => 'code'
), array());
然后我可以在 curl 中使用請求來獲取我的訪問令牌:
curl \
--request POST \
--data "code=[Authentcation code from authorization link]&client_id=[Application Client Id]&client_secret=[Application Client Secret]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
https://accounts.google.com/o/oauth2/token */
$tokenJson = json_decode( GoogleTokenCurl("POST", '', array(), array(
'code' => '4/1AX4XfWiEWngRngF7qryjtkcOG1otVtisYpjHnej1E54Pujcrchef8REvdt0',
'client_id' => $GOOGLE_CLIENT_ID,
'client_secret' => $GOOGLE_CLIENT_SECRET,
'redirect_uri'=>'urn:ietf:wg:oauth:2.0:oob',
'grant_type' => 'authorization_code'
)
));
print_r($tokenJson);
到目前為止,我已經為我的授權標題準備好了食物。我的問題是第一步(征得用戶同意)。我希望我可以在不將我的 url 放入瀏覽器的情況下執行此步驟,在獲取授權碼之前驗證兩個螢屏以授予訪問權限。
我也對創建帶有由 curl 驅動的休息請求的 gmail 訊息的建議感興趣。我發現郵遞員收集了有關 gmail api 可以執行的所有操作的資訊,但是一兩個呼叫示例不會造成傷害;)
謝謝 !
uj5u.com熱心網友回復:
在當前狀態下,根據您使用的方法&response_type=code,您需要兩次呼叫 OAuth 客戶端來獲取訪問令牌。您可以在此處找到如何僅使用HTTP/REST請求來處理它的示例。
在任何情況下,您都可以使用Google API Client Library for PHP。允許您處理 OAuth 身份驗證流程,只需要一次互動即可獲取令牌。
您可以在此處找到有關其作業原理的完整示例,請注意此示例使用 Drive API,如果您想在 Gmail API 中使用它,您可以查看Gmail API PHP 庫。
檔案:
- PHP Gmail API
- 用于訪問 Google API 的 OAuth 2.0
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/373463.html
