PHP code example of octoauth / oauth2-twitter-adapter

1. Go to this page and download the library: Download octoauth/oauth2-twitter-adapter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

octoauth / oauth2-twitter-adapter example snippets



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;


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();


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); 


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);