effectra/third-party

Effectra ThirdParty 包。

v1.0.0 2023-06-19 18:15 UTC

This package is auto-updated.

Last update: 2024-09-19 23:11:46 UTC


README

Effectra\ThirdParty 是一个PHP库,它为LinkedIn、GitHub、Facebook和Google等第三方平台提供了OAuth配置和功能。它简化了与这些平台的集成过程以及通过OAuth身份验证访问用户数据。

特性

  • 简化第三方平台的OAuth配置和身份验证。
  • 轻松检索访问令牌和用户信息。
  • 支持LinkedIn、GitHub、Facebook和Google等多个流行平台。

安装

您可以通过Composer安装Effectra\ThirdParty库。在您的项目目录中运行以下命令

composer require effectra/third-party

使用方法

LinkedIn

要使用LinkedIn OAuth功能,请按照以下步骤操作

  1. 通过提供您的LinkedIn客户端ID、客户端密钥以及可选的重定向URL和作用域来创建LinkedIn类的实例。
use Effectra\ThirdParty\LinkedIn;

$linkedin = new LinkedIn('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'YOUR_REDIRECT_URL', ['r_liteprofile', 'r_emailaddress']);
  1. 生成用于身份验证的重定向URL
$authUrl = $linkedin->getAuthURL();
  1. 将用户重定向到生成的授权URL。身份验证成功后,LinkedIn会将用户重定向回指定的重定向URL,并附带授权代码。

  2. 交换授权代码以获取访问令牌

$code = $_GET['code']; // The authorization code obtained from the LinkedIn redirect
$accessToken = $linkedin->getAccessToken($code);
  1. 使用访问令牌检索用户信息
$user = $linkedin->getUser($accessToken);

GitHub

要使用GitHub OAuth功能,请按照以下步骤操作

  1. 通过提供您的GitHub客户端ID、客户端密钥以及可选的重定向URL和作用域来创建GitHub类的实例。
use Effectra\ThirdParty\GitHub;

$github = new GitHub('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'YOUR_REDIRECT_URL', ['user']);
  1. 生成用于身份验证的重定向URL
$authUrl = $github->getAuthURL();
  1. 将用户重定向到生成的授权URL。身份验证成功后,GitHub会将用户重定向回指定的重定向URL,并附带授权代码。

  2. 交换授权代码以获取访问令牌

$code = $_GET['code']; // The authorization code obtained from the GitHub redirect
$accessToken = $github->getAccessToken($code);
  1. 使用访问令牌检索用户信息
$user = $github->getUser($accessToken);

Facebook

要使用Facebook OAuth功能,请按照以下步骤操作

  1. 通过提供您的Facebook客户端ID、客户端密钥以及可选的重定向URL和作用域来创建Facebook类的实例。
use Effectra\ThirdParty\Facebook;

$facebook = new Facebook('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'YOUR_REDIRECT_URL', ['email']);
  1. 生成用于身份验证的重定向URL
$authUrl = $facebook->getAuthURL();
  1. 将用户重定向到生成的授权URL。身份验证成功后,Facebook会将用户重定向回指定的重定向URL,并附带授权代码。

  2. 交换授权代码以获取访问令牌

$code = $_GET['code']; // The authorization code obtained from the Facebook redirect
$accessToken = $facebook->getAccessToken($code);

5

使用访问令牌检索用户信息

$user = $facebook->getUser($accessToken);

Google

要使用Google OAuth功能,请按照以下步骤操作

  1. 通过提供您的Google客户端ID、客户端密钥以及可选的重定向URL和作用域来创建Google类的实例。
use Effectra\ThirdParty\Google;

$google = new Google('YOUR_CLIENT_ID', 'YOUR_CLIENT_SECRET', 'YOUR_REDIRECT_URL', ['profile', 'email']);
  1. 生成用于身份验证的重定向URL
$authUrl = $google->getAuthURL();
  1. 将用户重定向到生成的授权URL。身份验证成功后,Google会将用户重定向回指定的重定向URL,并附带授权代码。

  2. 交换授权代码以获取访问令牌

$code = $_GET['code']; // The authorization code obtained from the Google redirect
$accessToken = $google->getAccessToken($code);
  1. 使用访问令牌检索用户信息
$user = $google->getUser($accessToken);

OAuthServiceInterface

OAuthServiceInterface 是一个定义OAuth服务合同的接口。它提供了从OAuth服务中检索配置、授权URL、访问令牌和用户数据的方法。

使用方法

要使用此接口,您需要创建一个实现它的类并提供必要的功能。以下是如何实现OAuthServiceInterface的示例

<?php

namespace Effectra\ThirdParty;

class MyOAuthService implements OAuthServiceInterface
{
    // Implement the getConfig() method
    public function getConfig(): array
    {
        // Return the configuration array for the OAuth service
    }

    // Implement the getAuthURL() method
    public function getAuthURL(): string
    {
        // Return the authorization URL for the OAuth service
    }

    // Implement the getAccessToken() method
    public function getAccessToken(string $code): string
    {
        // Get the access token for the OAuth service using the authorization code
    }

    // Implement the getUser() method
    public function getUser(string $token): ?array
    {
        // Get the user data from the OAuth service using the access token
    }
}

在上面的示例中,您需要用您根据所集成的OAuth服务实现的实际方法替换占位符方法。

方法概述

getConfig(): array

此方法返回OAuth服务的配置数组。

getAuthURL(): string

此方法返回OAuth服务的授权URL。

getAccessToken(string $code): string

此方法使用提供的授权码检索OAuth服务的访问令牌。

getUser(string $token): ?array

此方法使用提供的访问令牌从OAuth服务检索用户数据。它返回包含用户数据的数组,如果操作失败则返回null

许可证

此库是开源的,可在MIT许可证下使用。

贡献

欢迎贡献!如果您遇到任何问题或有改进建议,请随时创建一个问题或提交一个拉取请求。

鸣谢

此库由Effectra开发和维护。您可以在我们的网站上找到更多关于我们的信息:www.effectra.com

联系方式

对于任何查询或问题,您可以通过info@effectra.com联系我们

请随意根据您的具体需求修改和自定义此README文件。