Abstract Class yii\authclient\OAuth1

Inheritanceyii\authclient\OAuth1 » yii\authclient\BaseOAuth » yii\authclient\BaseClient » yii\base\Component
Implementsyii\authclient\ClientInterface
Subclassesyii\authclient\clients\Twitter
Available since version2.0

OAuth1 serves as a client for the OAuth 1/1.0a flow.

In order to acquire access token perform following sequence:

use yii\authclient\OAuth1;
use Yii;

// assuming class MyAuthClient extends OAuth1
$oauthClient = new MyAuthClient();
$requestToken = $oauthClient->fetchRequestToken(); // Get request token
$url = $oauthClient->buildAuthUrl($requestToken); // Get authorization URL
return Yii::$app->getResponse()->redirect($url); // Redirect to authorization URL

// After user returns at our site:
$accessToken = $oauthClient->fetchAccessToken(Yii::$app->request->get('oauth_token'), $requestToken); // Upgrade to access token

See also https://oauth.net/1/ https://tools.ietf.org/html/rfc5849.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$accessTokenMethod string Access token HTTP method. yii\authclient\OAuth1
$accessTokenUrl string OAuth access token URL. yii\authclient\OAuth1
$apiBaseUrl string API base URL. yii\authclient\BaseOAuth
$authUrl string Authorize URL. yii\authclient\BaseOAuth
$authorizationHeaderMethods array|null List of the request methods, which require adding 'Authorization' header. yii\authclient\OAuth1
$autoRefreshAccessToken boolean Whether to automatically perform 'refresh access token' request on expired access token. yii\authclient\BaseOAuth
$consumerKey string OAuth consumer key. yii\authclient\OAuth1
$consumerSecret string OAuth consumer secret. yii\authclient\OAuth1
$requestTokenMethod string Request token HTTP method. yii\authclient\OAuth1
$requestTokenUrl string OAuth request token URL. yii\authclient\OAuth1
$scope string Auth request scope. yii\authclient\BaseOAuth
$version string Protocol version. yii\authclient\OAuth1

Public Methods

Hide inherited methods

MethodDescriptionDefined By
api() Performs request to the OAuth API returning response data. yii\authclient\BaseOAuth
applyAccessTokenToRequest() Applies access token to the HTTP request instance. yii\authclient\OAuth1
beforeApiRequestSend() Handles \yii\httpclient\Request::EVENT_BEFORE_SEND event. yii\authclient\BaseOAuth
beforeRequestSend() Handles \yii\httpclient\Request::EVENT_BEFORE_SEND event. yii\authclient\OAuth1
buildAuthUrl() Composes user authorization URL. yii\authclient\OAuth1
createApiRequest() Creates an HTTP request for the API call. yii\authclient\OAuth1
createRequest() Creates HTTP request instance. yii\authclient\OAuth1
fetchAccessToken() Fetches OAuth access token. yii\authclient\OAuth1
fetchRequestToken() Fetches the OAuth request token. yii\authclient\OAuth1
getAccessToken() yii\authclient\BaseOAuth
getHttpClient() Returns HTTP client. yii\authclient\BaseClient
getId() yii\authclient\ClientInterface
getName() yii\authclient\ClientInterface
getNormalizeUserAttributeMap() yii\authclient\BaseClient
getRequestOptions() yii\authclient\BaseClient
getReturnUrl() yii\authclient\BaseOAuth
getSignatureMethod() yii\authclient\BaseOAuth
getStateStorage() yii\authclient\BaseClient
getTitle() yii\authclient\ClientInterface
getUserAttributes() yii\authclient\ClientInterface
getViewOptions() yii\authclient\ClientInterface
refreshAccessToken() Gets new auth token to replace expired one. yii\authclient\OAuth1
setAccessToken() Sets access token to be used. yii\authclient\BaseOAuth
setHttpClient() Sets HTTP client to be used. yii\authclient\BaseOAuth
setId() yii\authclient\ClientInterface
setName() yii\authclient\ClientInterface
setNormalizeUserAttributeMap() yii\authclient\BaseClient
setRequestOptions() yii\authclient\BaseClient
setReturnUrl() yii\authclient\BaseOAuth
setSignatureMethod() Set signature method to be used. yii\authclient\BaseOAuth
setStateStorage() yii\authclient\BaseClient
setTitle() yii\authclient\ClientInterface
setUserAttributes() yii\authclient\BaseClient
setViewOptions() yii\authclient\BaseClient
signRequest() Sign given request with \yii\authclient\signatureMethod. yii\authclient\OAuth1

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
composeAuthorizationHeader() Composes authorization header. yii\authclient\OAuth1
composeSignatureBaseString() Creates signature base string, which will be signed by \yii\authclient\signatureMethod. yii\authclient\OAuth1
composeSignatureKey() Composes request signature key. yii\authclient\OAuth1
composeUrl() Composes URL from base URL and GET params. yii\authclient\BaseOAuth
createHttpClient() Creates HTTP client instance from reference or configuration. yii\authclient\BaseOAuth
createSignatureMethod() Creates signature method instance from its configuration. yii\authclient\BaseOAuth
createToken() Creates token from its configuration. yii\authclient\BaseOAuth
defaultName() Generates service name. yii\authclient\BaseClient
defaultNormalizeUserAttributeMap() Returns the default \yii\authclient\normalizeUserAttributeMap value. yii\authclient\BaseClient
defaultRequestOptions() Returns default HTTP request options. yii\authclient\BaseOAuth
defaultReturnUrl() Composes default \yii\authclient\returnUrl value. yii\authclient\OAuth1
defaultTitle() Generates service title. yii\authclient\BaseClient
defaultViewOptions() Returns the default \yii\authclient\viewOptions value. yii\authclient\BaseClient
generateCommonRequestParams() Generate common request params like version, timestamp etc. yii\authclient\OAuth1
generateNonce() Generates nonce value. yii\authclient\OAuth1
generateTimestamp() Generates timestamp. yii\authclient\OAuth1
getState() Returns persistent state value. yii\authclient\BaseClient
getStateKeyPrefix() Returns session key prefix, which is used to store internal states. yii\authclient\BaseClient
initUserAttributes() Initializes authenticated user attributes. yii\authclient\BaseClient
normalizeUserAttributes() Normalize given user attributes according to \yii\authclient\normalizeUserAttributeMap. yii\authclient\BaseClient
removeState() Removes persistent state value. yii\authclient\BaseClient
restoreAccessToken() Restores access token. yii\authclient\BaseOAuth
saveAccessToken() Saves token as persistent state. yii\authclient\BaseOAuth
sendRequest() Sends the given HTTP request, returning response data. yii\authclient\BaseOAuth
setState() Sets persistent state. yii\authclient\BaseClient

Property Details

$accessTokenMethod public property

Access token HTTP method.

public string $accessTokenMethod 'GET'
$accessTokenUrl public property

OAuth access token URL.

public string $accessTokenUrl null
$authorizationHeaderMethods public property (available since version 2.1.1)

List of the request methods, which require adding 'Authorization' header. By default only POST requests will have 'Authorization' header. You may set this option to null in order to make all requests to use 'Authorization' header.

$consumerKey public property

OAuth consumer key.

public string $consumerKey null
$consumerSecret public property

OAuth consumer secret.

public string $consumerSecret null
$requestTokenMethod public property

Request token HTTP method.

public string $requestTokenMethod 'GET'
$requestTokenUrl public property

OAuth request token URL.

public string $requestTokenUrl null
$version public property

Protocol version.

public string $version '1.0'

Method Details

applyAccessTokenToRequest() public method

Applies access token to the HTTP request instance.

public void applyAccessTokenToRequest ( $request, $accessToken )
$request \yii\httpclient\Request

HTTP request instance.

$accessToken yii\authclient\OAuthToken

Access token instance.

beforeRequestSend() public method (available since version 2.1)

Handles \yii\httpclient\Request::EVENT_BEFORE_SEND event.

Ensures every request has been signed up before sending.

public void beforeRequestSend ( $event )
$event \yii\httpclient\RequestEvent

Event instance.

buildAuthUrl() public method

Composes user authorization URL.

public string buildAuthUrl ( yii\authclient\OAuthToken $requestToken null, array $params = [] )
$requestToken yii\authclient\OAuthToken

OAuth request token.

$params array

Additional request params.

return string

Authorize URL

throws \yii\base\InvalidParamException

on failure.

composeAuthorizationHeader() protected method

Composes authorization header.

protected array composeAuthorizationHeader ( array $params, $realm '' )
$params array

Request params.

$realm string

Authorization realm.

return array

Authorization header in format: [name => content].

composeSignatureBaseString() protected method

Creates signature base string, which will be signed by \yii\authclient\signatureMethod.

protected string composeSignatureBaseString ( $method, $url, array $params )
$method string

Request method.

$url string

Request URL.

$params array

Request params.

return string

Base signature string.

composeSignatureKey() protected method

Composes request signature key.

protected string composeSignatureKey ( $token null )
$token yii\authclient\OAuthToken|null

OAuth token to be used for signature key.

return string

Signature key.

createApiRequest() public method

Creates an HTTP request for the API call.

The created request will be automatically processed adding access token parameters and signature before sending. You may use createRequest() to gain full control over request composition and execution.

public \yii\httpclient\Request createApiRequest ( )
return \yii\httpclient\Request

HTTP request instance.

createRequest() public method

Creates HTTP request instance.

public \yii\httpclient\Request createRequest ( )
return \yii\httpclient\Request

HTTP request instance.

defaultReturnUrl() protected method

Composes default \yii\authclient\returnUrl value.

protected string defaultReturnUrl ( )
return string

Return URL.

fetchAccessToken() public method

Fetches OAuth access token.

public yii\authclient\OAuthToken fetchAccessToken ( $oauthToken null, yii\authclient\OAuthToken $requestToken null, $oauthVerifier null, array $params = [] )
$oauthToken string

OAuth token returned with redirection back to client.

$requestToken yii\authclient\OAuthToken

OAuth request token.

$oauthVerifier string

OAuth verifier.

$params array

Additional request params.

return yii\authclient\OAuthToken

OAuth access token.

throws \yii\base\InvalidParamException

on failure.

throws \yii\web\HttpException

in case oauth token miss-matches request token.

fetchRequestToken() public method

Fetches the OAuth request token.

public yii\authclient\OAuthToken fetchRequestToken ( array $params = [] )
$params array

Additional request params.

return yii\authclient\OAuthToken

Request token.

generateCommonRequestParams() protected method

Generate common request params like version, timestamp etc.

protected array generateCommonRequestParams ( )
return array

Common request params.

generateNonce() protected method

Generates nonce value.

protected string generateNonce ( )
return string

Nonce value.

generateTimestamp() protected method

Generates timestamp.

protected integer generateTimestamp ( )
return integer

Timestamp.

refreshAccessToken() public method

Gets new auth token to replace expired one.

public yii\authclient\OAuthToken refreshAccessToken ( yii\authclient\OAuthToken $token )
$token yii\authclient\OAuthToken

Expired auth token.

return yii\authclient\OAuthToken

New auth token.

signRequest() public method (available since version 2.1)

Sign given request with \yii\authclient\signatureMethod.

public void signRequest ( $request, $token null )
$request \yii\httpclient\Request

Request instance.

$token yii\authclient\OAuthToken|null

OAuth token to be used for signature, if not set \yii\authclient\accessToken will be used.