【发布时间】:2022-01-08 17:59:02
【问题描述】:
我想使用 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 可以执行的所有操作,但一两个调用示例不会造成伤害;)
谢谢!
【问题讨论】:
-
您考虑过使用google php 客户端库吗?它会自动为您在浏览器中打开网址。
-
不情愿,因为我维护的代码是程序性的,没有作曲家功能。
标签: php rest curl google-api gmail-api