【问题标题】:go through browser auth with rest requests - Gmail API通过浏览器身份验证和休息请求 - Gmail API
【发布时间】: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


【解决方案1】:

在当前状态下,通过您使用的方法&response_type=code,您需要两次调用 OAuth 客户端来获取访问令牌。您可以找到一个示例,说明如何仅使用 HTTP/REST requests here 来处理它。

无论如何,您都可以使用Google API Client Library for PHP。允许您处理 OAuth 身份验证流程,只需一次交互即可获取令牌。

您可以找到有关其工作原理的完整示例here,请注意此示例使用 Drive API,如果您想在 Gmail API 中使用它,可以查看Gmail API PHP library

文档:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-04
    • 2013-04-28
    • 2021-10-20
    • 2018-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    相关资源
    最近更新 更多