简体   繁体   中英

OAuth getRequestToken with custom parameters

Im trying to use PHP OAuth library and specifically the getRequestToken() method. A problem I've discovered is that I cant add custom parameters to my request. I would like to add the parameter "scope" but I can't find any way to do that. If I try to add the parameter to the URL it seems like all other parameters are failing like so:

$oauth->getRequestToken( "http://www.api.com/oauth/request_token?scope=email", "http://www.mypage.com/callback.php" );

This is a full example of a working API call but without the needed custom parameter:

$oauth = new \OAuth( "consumer_key", "consumer_secret" );
$oauth->setNonce( sha1( \Str::Random( 10 ).time() ) );
$oauth->setTimestamp( time() );

try
{
    $oauth->getRequestToken( "http://www.api.com/oauth/request_token", "http://www.mypage.com/callback.php" );
}
catch ( \OAuthException $e )
{
    exit( $oauth->getLastResponse() );
}

So: How do I add custom parameters to a OAuth call with the PHP OAuth library?

I've figured out what was missing if someone else encounter this problem. The solution is a combination of the methods "fetch" and "setAuthType". The method fetch() allows custom data as a second parameter. This way I could add my "scope" parameter like so:

$oauth->fetch( "http://www.api.com/oauth/request_token", array( "scope" => "email" ), OAUTH_HTTP_METHOD_GET );

This combined with the method setAuthType() made it possible. setAuthType() need a constant named OAUTH_AUTH_TYPE_*. For this to work I added the constant OAUTH_AUTH_TYPE_URI which appends the OAuth parameters to the request URI.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM