poposki / goodreads
此包已被废弃且不再维护。未建议替代包。
Goodreads API 的 PHP 封装器
v1.0.1
2017-12-09 18:06 UTC
Requires
- php: >=5.6
- league/oauth1-client: ^1.7
Requires (Dev)
This package is not auto-updated.
Last update: 2021-09-04 13:02:45 UTC
README
用于消费 Goodreads API 的 PHP 客户端。
安装
通过 Composer
$ composer require poposki/goodreads
使用
本指南将帮助您配置客户端(配置客户端)、验证用户并存储访问令牌)以及代表用户访问 API。
完整的提供者文档可在API 指南中找到。
在继续之前,请确保已安全存储您的 Goodreads API 密钥。您可以在 Goodreads 的API 文档和开发者密钥部分进行查看。
此项目包括一个基本 API 示例和一个 OAuth 授权示例。
配置客户端
Goodreads 提供者需要一些配置设置才能成功运行。
设置 | 描述 |
---|---|
identifier |
必需,与您的应用程序关联的应用程序密钥。 |
secret |
必需,与您的应用程序关联的应用程序密钥。 |
callback_uri |
在使用包时需要,有助于获取访问令牌。 |
oauth_token |
在使用包时需要,用于代表用户进行身份验证的 API 请求。 |
oauth_token_secret |
在使用包时需要,用于代表用户进行身份验证的 API 请求。 |
在创建提供者时设置配置
$provider = new \Poposki\Goodreads\Provider([ 'identifier' => 'your-application-key', 'secret' => 'your-application-secret', 'callback_uri' => 'http://your-callback-uri/', 'oauth_token' => 'abcdefghijklmnopqrstuvwxyz', 'oauth_token_secret' => 'abcdefghijklmnopqrstuvwxyz', ]);
验证用户并存储访问令牌
Goodreads 提供者可以帮助您引导用户完成 OAuth 授权过程,并向您的应用程序提供访问令牌凭据。
此包利用 The League's OAuth1 Client 提供此帮助。
// Create a provider instance. $provider = new \Poposki\Goodreads\Provider([ 'identifier' => 'your-application-key', 'secret' => 'your-application-secret', 'callback_uri' => 'http://your-callback-uri/', ]); // Obtain Temporary Credentials and User Authorization // Goodreads does not use an oauth_verifier, instead they have an authorize param // If the authorize param is 1, user has granted access, otherwise user denied access if (!isset($_GET['oauth_token'], $_GET['authorize']) || $_GET['authorize'] != 1) { // First part of OAuth 1.0 authentication is to // obtain Temporary Credentials. $temporaryCredentials = $provider->getTemporaryCredentials(); // Store credentials in the session, we'll need them later $_SESSION['temporary_credentials'] = serialize($temporaryCredentials); session_write_close(); // Second part of OAuth 1.0 authentication is to obtain User Authorization // by redirecting the resource owner to the login screen on the server. // Create an authorization url. $authorizationUrl = $provider->getAuthorizationUrl($temporaryCredentials); // Redirect the user to the authorization URL. The user will be redirected // to the familiar login screen on the server, where they will login to // their account and authorize your app to access their data. header('Location: ' . $authorizationUrl); exit; // Obtain Token Credentials } else { try { // Retrieve the temporary credentials we saved before. $temporaryCredentials = unserialize($_SESSION['temporary_credentials']); // We will now obtain Token Credentials from the server. $tokenCredentials = $provider->getTokenCredentials( $temporaryCredentials, $_GET['oauth_token'] ); // We have token credentials, which we may use in authenticated // requests against the service provider's API. echo $tokenCredentials->getIdentifier() . "\n"; echo $tokenCredentials->getSecret() . "\n"; // Store token credentials so that you can use them for authorized requests later on // ... } catch (\Exception $e) { // Failed to get the token credentials or user details. exit($e->getMessage()); } }
使用访问令牌访问 API
$provider = new \Poposki\Goodreads\Provider([ 'identifier' => 'your-application-key', 'secret' => 'your-application-secret', 'oauth_token' => 'user-oauth-token', 'oauth_token_secret' => 'user-oauth-token-secret', ]); try { // Get id of user who authorized OAuth $user = $provider->getUserId(); // Get info about an author by id $author = $provider->getAuthorById(7160538); } catch (\Exception $e) { // Failed to get the token credentials. exit($e->getMessage()); }
API 指南中大部分方法都需要实体 ID 来执行业务。
许可
在 MIT 许可证下许可 - 有关详细信息,请参阅LICENSE 文件。