Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
157 lines (131 sloc) 4.16 KB

OAuth1Client

The OAuth1Client allows you to connect to any OAuth1 server. Just follow the procedure described in the example below.

Installation

composer require webiik/oauth1client

Example

// Twitter Example

// Prepare dependencies
$chc = new \Webiik\CurlHttpClient\CurlHttpClient();
$token = new \Webiik\Token\Token();

// Instantiate OAuth1 client
$oAuth1Client = new \Webiik\OAuth1Client\OAuth1Client($chc, $token);

// Set your callback URL
// OAuth1 server redirects users to this URL, after user verification 
$oAuth1Client->setCallbackUrl('https://127.0.0.1/webiik/');

// Set OAuth1 server endpoints
$oAuth1Client->setAuthorizeUrl('https://api.twitter.com/oauth/authenticate');
$oAuth1Client->setRequestTokenUrl('https://api.twitter.com/oauth/request_token');
$oAuth1Client->setAccessTokenUrl('https://api.twitter.com/oauth/access_token');

// Set OAuth1 server access credentials (create yours at https://developer.twitter.com/en/apps)
$oAuth1Client->setConsumerKey('your-api-key');
$oAuth1Client->setConsumerSecret('your-api-secret');

// Make API calls...
// Notice: It's a good idea to separate below steps to individual routes. 

if (!isset($_GET['oauth_verifier'])) {
    // 1. Prepare Twitter login link   
    echo '<a href="' . $oAuth1Client->getAuthorizeUrl() . '" target="_blank">Authorize with Twitter</a><br/>';
}

if (isset($_GET['oauth_verifier'])) {
    // 2. Verify oauth_token
    $accessToken = $oAuth1Client->getAccessToken();
}

if (isset($accessToken['oauth_token'])) {
    // 3. oauth_token is valid, user is authorized by Twitter
    // Access protected resources...
}

Configuration

Before you can connect to any OAuth1 server, you have to properly configure access credentials and endpoints.

setConsumerKey

setConsumerKey(string $key): void

setConsumerKey() sets consumer key.

$oAuth1Client->setConsumerKey('your-api-key');

setConsumerSecret

setConsumerSecret(string $secret): void

setConsumerSecret() sets consumer secret.

$oAuth1Client->setConsumerSecret('your-api-secret');

setSignatureSecret

setSignatureSecret(string $secret): void

setSignatureSecret() sets signature secret. Usually it't optional or not required.

$oAuth1Client->setSignatureSecret('your-api-signature-secret');

setRequestTokenUrl

setRequestTokenUrl(string $url): void

setRequestTokenUrl() sets URL to obtain a request token.

$oAuth1Client->setRequestTokenUrl('https://api.twitter.com/oauth/request_token');

setAuthorizeUrl

setAuthorizeUrl(string $url): void

setAuthorizeUrl() sets URL to authorize a request token by user at OAuth1 server.

$oAuth1Client->setAuthorizeUrl('https://api.twitter.com/oauth/authenticate');

setCallbackUrl

setCallbackUrl(string $url): void

setCallbackUrl() sets URL to redirect a user after authorization.

$oAuth1Client->setCallbackUrl('https://127.0.0.1/webiik/');

setAccessTokenUrl

setAccessTokenUrl(string $url): void

setAccessTokenUrl() sets URL to obtain a access token.

$oAuth1Client->setAccessTokenUrl('https://api.twitter.com/oauth/access_token');

Login

getAuthorizeUrl

getAuthorizeUrl($requestToken): string

getAuthorizeUrl() makes HTTP POST request to a URL set by setRequestTokenUrl() and prepares authorized link to a URL set by setAuthorizeUrl().

$link = $oAuth1Client->getAuthorizeUrl();

Authorization

getAccessToken

getAccessToken()

getAccessToken() makes HTTP POST request to URL set by setAccessTokenUrl() and returns an array with 'oauth_token' on success and a string with cURL error message on error.

$accessToken = $oAuth1Client->getAccessToken();

Resources

You can’t perform that action at this time.