octoauth / oauth2-twitter-adapter
一个适配器,允许您在与 'phpleague/oauth2-*' 库一起使用时,与 'abraham/twitteroauth' OAuth1 库进行交互
Requires
- abraham/twitteroauth: ^0.7.2
- league/oauth2-client: ~1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^1.11.2
- phpdocumentor/phpdocumentor: 2.*
This package is not auto-updated.
Last update: 2024-09-29 02:34:06 UTC
README
Twitter 明显不在 phpleague/oauth2-client 支持的 OAuth2 提供商列表中 支持。这是因为 Twitter 不支持 OAuth2 用户认证,尽管他们提供 OAuth 1.0 支持。
此软件包提供了一个用于出色的 abraham/twitteroauth
库(使用 OAuth 1.0 与 Twitter 交互)的包装器,使其能够更容易地与各种 PhpLeague 的 OAuth2 客户端一起使用。
安装
要安装,请使用 composer
./composer.phar require octoauth/oauth2-twitter-adapter
使用方法
开始认证
实例化一个 TwitterOAuth1
客户端,传递一个机制以在页面请求之间保留 OAuth1 临时令牌。提供了一个基于会话的存储,但如果你运行的是无会话应用程序,你可以非常容易地创建自己的 OAuth1TemporaryTokenStore
实现,使用数据库或其他方式
<?php use OctOAuth\OAuth2\Client\Provider\Token\OAuth1TemporaryTokenStoreOnSession; use OctOAuth\OAuth2\Client\Provider\Twitter\TwitterOAuth1; $provider = new TwitterOAuth1( [ // NOTE that the credentials array matches the format of phpleague/oauth2-clients 'clientId' => "YOUR_TWITTER_KEY", 'clientSecret' => "YOUR_TWITTER_SECRET", 'redirectUri' => "https://your-app.com/oauthCallbackPage?Provider=Twitter", ], new OAuth1TemporaryTokenStoreOnSession()); // You can pass-in an array of options such as scope for OAuth2 providers, but they // aren't supported by the Twitter OAuth1 provider. You app's scope is configured // at https://apps.twitter.com/app $authURL = $provider->getAuthorizationUrl(); header('Location: {$authURL}', true, 303); exit;
你的用户现在将被发送到 OAuth 授权页面,并在他们批准或拒绝授权后,被重定向回上面配置的 redirectUri
。
完成认证
在你的 redirectUri
上,你应该像以前一样创建提供者(或从会话中加载它,如果你将其缓存),检查回调是否包含成功认证所需的一切,然后完成登录。到那时,你将有一个 AccessToken
,你可以按需使用它 - 在下面的示例中检索 ResourceOwner
。
<?php use OctOAuth\OAuth2\Client\Provider\Token\OAuth1TemporaryTokenStoreOnSession; use OctOAuth\OAuth2\Client\Provider\Twitter\TwitterOAuth1; $provider = new TwitterOAuth1( [ // NOTE that the credentials array matches the format of phpleague/oauth2-clients 'clientId' => "YOUR_TWITTER_KEY", 'clientSecret' => "YOUR_TWITTER_SECRET", 'redirectUri' => "https://your-app.com/oauthCallbackPage?Provider=Twitter", ], new OAuth1TemporaryTokenStoreOnSession()); // an IdentityProviderException will be thrown if the authorization failed $provider->checkCallback(); $authCode = $authCode = $provider->getAuthCodeFromCallback(); // and call Twitter to convert it to an AccessToken (which you'll likely want to store somewhere // for later use) $token = $provider->getAccessTokenFromAuthCode($authCode); // at this point you can call the Twitter API to get your ResourceOwner $resourceOwner = $provider->getResourceOwner($token); echo "Twitter user ID: " . $resourceOwner->getId(); echo "\nTwitter screen name: " . $resourceOwner->getScreenName();
进行 API 调用
现在你有了访问令牌,你可以代表用户调用 Twitter REST API
<?php use OctOAuth\OAuth2\Client\Provider\Token\OAuth1TemporaryTokenStoreOnSession; use OctOAuth\OAuth2\Client\Provider\Twitter\TwitterOAuth1; $provider = new TwitterOAuth1( [ // NOTE that the credentials array matches the format of phpleague/oauth2-clients 'clientId' => "YOUR_TWITTER_KEY", 'clientSecret' => "YOUR_TWITTER_SECRET", 'redirectUri' => "https://your-app.com/oauthCallbackPage?Provider=Twitter", ], new OAuth1TemporaryTokenStoreOnSession()); // retrieve your token from the session or wherever you put it when you completed // authorization, then... $client = $provider->getAuthenticatedConnection($token); $apiResponse = $client->get("search/tweets", ["q" => "twitterapi"]); echo "Retrieved Tweets: " . print_r($apiResponse, true);
简化与 OAuth2 客户端的配合使用
为了在不关心你是在使用 OAuth1.0 还是 OAuth2 提供商的情况下完成用户授权过程,将你的 phpleague/oauth2-client 实现包装在提供的 LeagueOAuth2Adapter
中,使其实现 ProviderInterface
。然后你可以像上面描述的那样与适配器交互
<?php use OctOAuth\OAuth2\Client\Provider\LeagueOAuth2Adapter; use League\OAuth2\Client\Provider\Google; // doesn't have to be Google - use any of the phpleague/oauth2-client implementations $provider = new LeagueOAuth2Adapter( new Google( [ 'clientId' => "YOUR_GOOGLE_KEY", 'clientSecret' => "YOUR_GOOGLE_SECRET", 'redirectUri' => "https://your-app.com/oauthCallbackPage?Provider=Google", ] )); // ... // go through the authorizations sequence described above... // ... // once you have a token you can get your ResourceOwner $resourceOwner = $provider->getResourceOwner($token); echo "Google user ID: " . $resourceOwner->getId(); echo "\nGoogle user's name: " . $resourceOwner->getName(); // you can also access the underlying provider to make authenticated calls to the APIs $googleProvider = $provider->getProvider(); $googleProvider->getAuthenticatedRequest("GET", "https://www.googleapis.com/plus/v1/people/me", $token);