oauth2 twitter
v 1.1.0
이 패키지는 PHP League의 OAuth 2.0 클라이언트에 대한 Twitter OAuth 2.0 지원을 제공합니다.
설치하려면 작곡가를 사용하십시오.
composer require smolblog/oauth2-twitter
사용법은 SmolblogOAuth2ClientProviderTwitter
공급자로 사용하는 The League의 OAuth 클라이언트와 동일합니다.
<?php
session_start ();
require_once ' vendor/autoload.php ' ;
$ provider = new Smolblog OAuth2 Client Provider Twitter ([
' clientId ' => ' MjVXMnRGVUN5Ym5lcVllcTVKZkk6MTpjaQ ' ,
' clientSecret ' => ' YDPiM-JsC5xU44P2VijGJRB7zdKB1PckCGjOynXGx9HZM7N6As ' ,
' redirectUri ' => ' http://oddevan.test/twitter-test/ ' ,
]);
if (! isset ( $ _GET [ ' code ' ])) {
unset( $ _SESSION [ ' oauth2state ' ]);
unset( $ _SESSION [ ' oauth2verifier ' ]);
// Optional: The default scopes are ‘tweet.read’, ‘users.read’,
// and ‘offline.access’. You can change them like this:
$ options = [
‘scope’ => [
‘tweet.read’,
‘tweet.write’,
‘tweet.moderate.write’,
‘users.read’,
‘follows.read’,
‘follows.write’,
‘offline.access’,
‘space.read’,
‘mute.read’,
‘mute.write’,
‘like.read’,
‘like.write’,
‘list.read’,
‘list.write’,
‘block.read’,
‘block.write’,
‘bookmark.read’,
‘bookmark.write’,
],
];
// If we don't have an authorization code then get one
$ authUrl = $ provider -> getAuthorizationUrl ( $ options );
$ _SESSION [ ' oauth2state ' ] = $ provider -> getState ();
// We also need to store the PKCE Verification code so we can send it with
// the authorization code request.
$ _SESSION [ ' oauth2verifier ' ] = $ provider -> getPkceVerifier ();
header ( ' Location: ' . $ authUrl );
exit;
// Check given state against previously stored one to mitigate CSRF attack
} elseif ( empty ( $ _GET [ ' state ' ]) || ( $ _GET [ ' state ' ] !== $ _SESSION [ ' oauth2state ' ])) {
unset( $ _SESSION [ ' oauth2state ' ]);
exit ( ' Invalid state ' );
} else {
try {
// Try to get an access token (using the authorization code grant)
$ token = $ provider -> getAccessToken ( ' authorization_code ' , [
' code ' => $ _GET [ ' code ' ],
' code_verifier ' => $ _SESSION [ ' oauth2verifier ' ],
]);
// Optional: Now you have a token you can look up a users profile data
// We got an access token, let's now get the user's details
$ user = $ provider -> getResourceOwner ( $ token );
// Use these details to create a new profile
printf ( ' Hello %s! ' , $ user -> getName ());
} catch ( Exception $ e ) {
echo ' <pre> ' ;
print_r ( $ e );
echo ' </pre> ' ;
// Failed to get user details
exit ( ' Oh dear... ' );
}
// Use this to interact with an API on the users behalf
echo $ token -> getToken ();
}
CHANGELOG.md
참조하세요.
Smolblog 프로젝트의 일부로 유지*되었습니다.
*Twitter의 새로운 유료 API를 사용하면 Smolblog 프로젝트는 더 이상 이 플러그인을 안정적으로 유지할 수 없습니다. 가능한 모든 문제를 해결하겠습니다. 하지만 더 이상 새로운 기능에 대응할 수 없습니다. 적극적인 유지 관리를 맡기고 싶다면 연락하세요.
수정된 3조 BSD 라이센스(BSD). 자세한 내용은 라이센스 파일을 참조하십시오.