soatok / patreon
通过 OAuth 与 Patreon API 交互。
v0.7.0
2021-08-09 04:25 UTC
Requires
- php: >=7.0.0
- ext-curl: *
- ext-json: *
- paragonie/certainty: ^2
- paragonie/hidden-string: ^1
- paragonie/sodium_compat: ^1
Requires (Dev)
- phpunit/phpunit: ^6|^7|^8
- squizlabs/php_codesniffer: ^3.0
- vimeo/psalm: ^2|^3
This package is auto-updated.
Last update: 2024-08-30 01:26:15 UTC
README
这是 Soatok 对 Patreon-PHP 库的分支。
通过 OAuth 与 Patreon API(版本 2)交互。 需要 PHP 7。
本库与 Patreon 的区别
- 本库需要 PHP 7 或更高版本,而 Patreon 只需要 PHP 5.3。
- 本库加载了 hidden-string 包(防止秘密在堆栈跟踪中泄漏)和 sodium_compat 包(用于确保即使以奇怪的方式,libsodium 也是可访问的--在标准化的 PHP 7.2+ API 中)。
- 如果可能,本库使用 BLAKE2b 而不是 MD5 进行响应缓存。
- 本库具有新的
Patreon\AuthUrl
类,它可以生成一个认证 URL,无需手动 URL 编码和连接字符串。- 这应该会使 PHP 开发人员更容易通过 OAuth 与 Patreon 集成。
安装
从 Packagist 获取插件
composer require soatok/patreon
尽管可以在不使用 Composer 的情况下加载此库,但强烈建议您使用 Composer,请参阅 此指南。
Soatok 将不支持非 Composer 安装。
用法
步骤 1. 获取你的 client_id 和 client_secret
以 Patreon 创作者的身份登录并访问 Patreon 平台文档页面 注册你的客户端。
这将为您提供 client_id
和 client_secret
。
步骤 2. 在你的代码中使用此插件
假设你想要创建一个 "使用 Patreon 登录" 按钮。
你已经阅读了 说明,并尝试在服务器上实现 "步骤 2:处理 OAuth 重定向"。
用户将在你将他们发送到 授权页面 之后的步骤 1 后到达你的一个页面,因此在他们查询参数到达此页面的过程中,他们将有一个参数 'code'
。
(如果你不是在实现 "使用 Patreon 登录" 流,请参阅 示例文件夹 获取更多示例。)
(特别是统一流程是一个很好的方式,让用户在你的网站或应用中解锁受保护的功能或内容 - 它允许用户注册、登录、承诺并在一个流畅的统一流程中返回你的应用。在 示例文件夹 中查看。)
<?php // This example shows how to have your users log in via Patreon, and acquire access and refresh tokens after logging in require_once __DIR__.'/vendor/autoload.php'; use Patreon\{ API, AuthUrl, OAuth }; $client_id = ''; // Replace with your data $client_secret = ''; // Replace with your data // Set the redirect url where the user will land after oAuth. // That url is where the access code will be sent as a _GET parameter. // This may be any url in your app that you can accept and process the access code and login // In this case, say, /patreon_login request uri $redirect_uri = "http://mydomain.com/patreon_login"; $href = (new AuthUrl($client_id)) ->withRedirectUri($redirect_uri); // You can send an array of vars to Patreon and receive them back as they are. Ie, state vars to set the user state, app state or any other info which should be sent back and forth. $state = array(); // For example lets set final page which the user needs to land at - this may be a content the user is unlocking via oauth, or a welcome/thank you page // Lets make it a thank you page $state['final_page'] = 'http://mydomain.com/thank_you'; // Add any number of vars you need to this array by $state['YOURKEY'] = VARIABLE $href = $href->withState($state); // Scopes! You must request the scopes you need to have the access token. // In this case, we are requesting the user's identity (basic user info), user's email // For example, if you do not request email scope while logging the user in, later you wont be able to get user's email via /identity endpoint when fetching the user details // You can only have access to data identified with the scopes you asked. Read more at https://docs.patreon.com/#scopes // Lets request identity of the user, and email. $href = $href ->withAddedScope('identity') ->withAddedScope('identity[email]'); // Simply echoing it here. You can present the login link/button in any other way. echo '<a href="'.$href.'">Click here to login via Patreon</a>'; // Up to this part we handled the way to prepare a login link for users to log in via Patreon oAuth using API v2. From this point on starts the processing of a logged in user or user returning from Patreon oAuth. // The below code snippet needs to be active wherever the the user is landing in $redirect_uri parameter above. It will grab the auth code from Patreon and get the tokens via the oAuth client if (!empty($_GET['code'])) { $oauth_client = new OAuth($client_id, $client_secret); $tokens = $oauth_client->get_tokens($_GET['code'], $redirect_uri); $access_token = $tokens['access_token']; $refresh_token = $tokens['refresh_token']; // Here, you should save the access and refresh tokens for this user somewhere. Conceptually this is the point either you link an existing user of your app with his/her Patreon account, or, if the user is a new user, create an account for him or her in your app, log him or her in, and then link this new account with the Patreon account. More or less a social login logic applies here. // Only use user's email address info coming from Patreon if the email is verified. Check for is_email_verified value in user's API return. } if (empty($access_token)) { exit; } // After linking an existing account or a new account with Patreon by saving and matching the tokens for a given user, you can then read the access token (from the database or whatever resource), and then just check if the user is logged into Patreon by using below code. Code from down below can be placed wherever in your app, it doesnt need to be in the redirect_uri at which the Patreon user ends after oAuth. You just need the $access_token for the current user and thats it. // Lets say you read $access_token for current user via db resource, or you just acquired it through oAuth earlier like the above - create a new API client $api_client = new API($access_token); // Return from the API can be received in either array, object or JSON formats by setting the return format. It defaults to array if not specifically set. Specifically setting return format is not necessary. Below is shown as an example of having the return parsed as an object. Default is array (associated) and there is no need to specifically set it if you are going to use it as an array. If there is anyone using Art4 JSON parser lib or any other parser, they can just set the API return to json and then have the return parsed by that parser // You dont need the below line if you are going to use the return as array. $api_client->api_return_format = 'object'; // Now get the current user: $patron_response = $api_client->fetch_user(); // At this point you can do anything with the user return. For example, if there is no return for this user, then you can consider the user not logged into Patreon. Or, if there is return, then you can get the user's Patreon id or pledge info. For example if you are able to acquire user's id, then you can consider the user logged into Patreon.