【问题标题】:Google OAuth2 authenticate and API on PHPGoogle OAuth2 在 PHP 上进行身份验证和 API
【发布时间】:2017-06-28 07:24:03
【问题描述】:

我创建了一个允许用户使用 Google 登录的按钮。

用户选择他们的谷歌账户后,我可以得到以下参数作为谷歌的响应。

访问令牌 id_token 过期日期在 令牌类型 已创建

如何使用这些参数来使用 Google 提供的服务?

例如,我可以为用户创建进入 gmail 邮箱的按钮吗?

如果不是办法,那这些token有什么用?

【问题讨论】:

    标签: php api oauth-2.0 token


    【解决方案1】:

    使用 Google 的令牌 ID,您可以获得用户的信息,例如姓名电子邮件。

    您无法提供直接选项来打开 Gmail。它会提示用户登录

    您可以参考此文档以获取更多信息Gmail scopes

    【讨论】:

    • 所以即使我得到了访问令牌的东西,我也不能让他们使用谷歌驱动器或 Gmail 等谷歌服务?因为实际上我在做类似单点登录的事情,我想给用户带来方便,这样他们就可以通过谷歌登录,并且可以提供链接到谷歌服务的按钮,而无需再次登录。
    【解决方案2】:

    要访问 Google 服务,您需要access_token。创建身份验证 URL 时,必须指定要在应用程序中使用的所有范围 - 权限(来自 this list)。然后,Google 会征求用户同意将这些权限委派给您的应用程序。您获得的访问令牌将允许您执行这些操作。

    您可以浏览这些 Google API 范围并了解您可以做什么,不可以做什么。例如,您不能使用 GMail GUI,但您可以阅读、发送、删除和管理用户的电子邮件,发送新电子邮件等。

    【讨论】:

    • 现在,我在范围内添加了驱动器。那么,如何使用 access_token 在 Google Drive 上执行操作呢?
    • 查看 Google Drive API 文档。甚至还有现成的 PHP 代码示例developers.google.com/drive/v3/web/quickstart/php
    • 是的,我已经按照教程一步步试过了。但是有错误,我无法弄清楚解决方案。在终端输入 php quickstart.php 后,出现如下错误提示
    • Fatal error: Uncaught exception 'InvalidArgumentException' with message 'missing the required redirect URI' in /Applications/XAMPP/xamppfiles/htdocs/sso-test/vendor/google/auth/src/OAuth2.php:639 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/sso-test/vendor/google/apiclient/src/Google/Client.php(340): Google\Auth\OAuth2->buildFullAuthorizationUri(Array) #1 /Applications/XAMPP/xamppfiles/htdocs/sso-test/quickstart.php(36): Google_Client->createAuthUrl() #2 /Applications/XAMPP/xamppfiles/htdocs/sso-test/quickstart.php(75): getClient() #3 {main}
    • thrown in /Applications/XAMPP/xamppfiles/htdocs/sso-test/vendor/google/auth/src/OAuth2.php on line 639
    猜你喜欢
    • 1970-01-01
    • 2013-06-24
    • 2023-03-29
    • 1970-01-01
    • 2021-01-16
    • 1970-01-01
    • 2012-11-20
    • 2015-03-24
    • 1970-01-01
    相关资源
    最近更新 更多