问题
I am currently stuck trying to make requests to a service's api using a 2 legged oAuth request using PHP.
I am using the PHP library found here: http://code.google.com/p/oauth-php/ and there seems to be absolutely no documentation anywhere online for using this library for a 2 legged request.
So currently from the service I have the following details:
- $consumer_key - needs to be an empty string
- $consumer_secret - needs to be an empty string
- $access_token - my login name
- $access_token_secret - your generated application token
And I want to be able to make a request to:
http://foo.com/api/test/whoami
To test that the authentication is working correctly so I can use the rest of the api.
Anybody got any pointers on how to use that php library to achieve this? or are there better methods for a simple 2 legged call like this?
Help!? :)
回答1:
This little example might help http://developer.yahoo.com/blogs/ydn/posts/2010/04/a_twolegged_oauth_serverclient_example/
回答2:
You need just the consumer key and consumer secret to make authorized 2-legged oauth requests.
Download OAuthConsumer. (comment out the class OAuthException
lines 6-8)
Code sample:
require_once 'OAuth.php';
$signatureMethod = new OAuthSignatureMethod_HMAC_SHA1();
$consumerKey = 'abc';
$consumerSecret = 'def';
$httpMethod = 'GET';
$url = 'http://path/to/endpoint';
$requestFields = array();
$oauthConsumer = new OAuthConsumer($consumerKey, $consumerSecret, NULL);
$oauthRequest = OAuthRequest::from_consumer_and_token($oauthConsumer, NULL, $httpMethod, $url, $requestFields);
$oauthRequest->sign_request($signatureMethod, $oauthConsumer, NULL);
来源:https://stackoverflow.com/questions/1496561/oauth-in-php-to-make-2-legged-request