PHP code example of risan / oauth1

1. Go to this page and download the library: Download risan/oauth1 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/ */

    

risan / oauth1 example snippets




declare(strict_types=1);

use Risan\OAuth1\OAuth1Factory;

ier' => $_ENV['OAUTH_CONSUMER_KEY'],
    'client_credentials_secret' => $_ENV['OAUTH_CONSUMER_SECRET'],
    'temporary_credentials_uri' => 'https://service.example/oauth/request_token',
    'authorization_uri' => 'https://service.example/oauth/authorize',
    'token_credentials_uri' => 'https://service.example/oauth/access_token',
    'callback_uri' => 'https://client.example/oauth/callback',
    'base_uri' => 'https://service.example/api/',
]);



declare(strict_types=1);

session_start();

$temporary = $oauth->requestTemporaryCredentials();

$_SESSION['oauth1_temporary'] = [
    'identifier' => $temporary->getIdentifier(),
    'secret' => $temporary->getSecret(),
];

header('Location: '.$oauth->buildAuthorizationUri($temporary), true, 302);
exit;



declare(strict_types=1);

use Risan\OAuth1\Credentials\TemporaryCredentials;

session_start();

if (!isset($_GET['oauth_token'], $_GET['oauth_verifier'], $_SESSION['oauth1_temporary'])) {
    throw new RuntimeException('The OAuth callback is incomplete.');
}

$stored = $_SESSION['oauth1_temporary'];
unset($_SESSION['oauth1_temporary']);

$temporary = new TemporaryCredentials($stored['identifier'], $stored['secret']);
$token = $oauth->requestTokenCredentials(
    $temporary,
    (string) $_GET['oauth_token'],
    (string) $_GET['oauth_verifier'],
);

$_SESSION['oauth1_token'] = [
    'identifier' => $token->getIdentifier(),
    'secret' => $token->getSecret(),
];

use Risan\OAuth1\Credentials\TokenCredentials;

$stored = $_SESSION['oauth1_token'];
$oauth->setTokenCredentials(new TokenCredentials($stored['identifier'], $stored['secret']));

$response = $oauth->get('account', [
    'query' => ['

$response = $oauth->post('items', [
    'form_params' => [
        'name' => 'Example',
        'enabled' => '1',
    ],
]);

$response = $oauth->get('search', [
    'query' => [
        ['tag', 'php'],
        ['tag', 'oauth'],
        ['empty', ''],
    ],
]);

$oauth = OAuth1Factory::create($config);

use Risan\OAuth1\Signature\RsaSha1Signer;

$privateKey = file_get_contents('/secure/path/oauth-private-key.pem');
$oauth = OAuth1Factory::create($config, new RsaSha1Signer($privateKey, $_ENV['KEY_PASSPHRASE']));

use Risan\OAuth1\Signature\PlainTextSigner;

$oauth = OAuth1Factory::create($config, new PlainTextSigner());

use Risan\OAuth1\ProviderFactory;

$oauth = ProviderFactory::tumblr([
    'client_credentials_identifier' => $_ENV['TUMBLR_CONSUMER_KEY'],
    'client_credentials_secret' => $_ENV['TUMBLR_CONSUMER_SECRET'],
    'callback_uri' => 'https://client.example/oauth/callback',
]);