我正在为网站开发一个邀请朋友工具。我阅读了这些文章并采取了以下步骤:
- http://anandafit.info/2011/01/06/yahoo-contact-list-reader-in-php/
- http://nullinfo.wordpress.com/oauth-yahoo/
但是当我执行该操作时,最后,雅虎说:
{ "error": { "lang": "en-US", "description": "Please provide valid credentials. OAuth oauth_problem=\"token_rejected\", realm=\"yahooapis.com\"" } }
:((
这些是我的代码:
getreqtok.php
<?php
require 'modules/invite/yahoo/globals.php';
require 'modules/invite/yahoo/oauth_helper.php';
// Callback can either be 'oob' or a url whose domain must match
// the domain that you entered when registering your application
$callback='http://www.warzone.in/modules.php?name=invite&op=yahoo_get_contacts';
// Get the request token using HTTP GET and HMAC-SHA1 signature
$retarr = get_request_token(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
$callback, false, true, true);
//echo $retarr[3]["oauth_token_secret"]."<br>
//<br>
//";
session_start();
$_SESSION["oauth_token_secret"] = $retarr[3]["oauth_token_secret"];
//echo $_SESSION["oauth_token_secret"]."<br>
//<br>
//<br>
//";
if (! empty($retarr)){
list($info, $headers, $body, $body_parsed) = $retarr;
if ($info['http_code'] == 200 && !empty($body)) {
//print "Have the user go to xoauth_request_auth_url to authorize your app\n";
?>
<a href="<?php echo rfc3986_decode($body_parsed['xoauth_request_auth_url'])?>">YAHOO</a>
<?php
}
}
/**
* Get a request token.
* @param string $consumer_key obtained when you registered your app
* @param string $consumer_secret obtained when you registered your app
* @param string $callback callback url can be the string 'oob'
* @param bool $usePost use HTTP POST instead of GET
* @param bool $useHmacSha1Sig use HMAC-SHA1 signature
* @param bool $passOAuthInHeader pass OAuth credentials in HTTP header
* @return array of response parameters or empty array on error
*/
function get_request_token($consumer_key, $consumer_secret, $callback, $usePost=false, $useHmacSha1Sig=true, $passOAuthInHeader=false)
{
$retarr = array(); // return value
$response = array();
$url = 'https://api.login.yahoo.com/oauth/v2/get_request_token';
$params['oauth_version'] = '1.0';
$params['oauth_nonce'] = mt_rand();
$params['oauth_timestamp'] = time();
$params['oauth_consumer_key'] = $consumer_key;
$params['oauth_callback'] = $callback;
// compute signature and add it to the params list
if ($useHmacSha1Sig) {
$params['oauth_signature_method'] = 'HMAC-SHA1';
$params['oauth_signature'] =
oauth_compute_hmac_sig($usePost? 'POST' : 'GET', $url, $params,
$consumer_secret, null);
} else {
$params['oauth_signature_method'] = 'PLAINTEXT';
$params['oauth_signature'] =
oauth_compute_plaintext_sig($consumer_secret, null);
}
// Pass OAuth credentials in a separate header or in the query string
if ($passOAuthInHeader) {
$query_parameter_string = oauth_http_build_query($params, FALSE);
$header = build_oauth_header($params, "yahooapis.com");
$headers[] = $header;
} else {
$query_parameter_string = oauth_http_build_query($params);
}
// POST or GET the request
if ($usePost) {
$request_url = $url;
logit("getreqtok:INFO:request_url:$request_url");
logit("getreqtok:INFO:post_body:$query_parameter_string");
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$response = do_post($request_url, $query_parameter_string, 443, $headers);
} else {
$request_url = $url . ($query_parameter_string ?
('?' . $query_parameter_string) : '' );
logit("getreqtok:INFO:request_url:$request_url");
$response = do_get($request_url, 443, $headers);
}
// extract successful response
if (! empty($response)) {
list($info, $header, $body) = $response;
$body_parsed = oauth_parse_str($body);
if (! empty($body_parsed)) {
logit("getreqtok:INFO:response_body_parsed:");
//print_r($body_parsed);
}
$retarr = $response;
$retarr[] = $body_parsed;
}
return $retarr;
}
?>
getacctok.php
<?php
session_start();
require 'modules/invite/yahoo/globals.php';
require 'modules/invite/yahoo/oauth_helper.php';
// Fill in the next 3 variables.
$request_token=$_REQUEST["oauth_token"];
$request_token_secret=$_SESSION["oauth_token_secret"];
$oauth_verifier= $_REQUEST["oauth_verifier"];
//echo $request_token." xxxx ".$request_token_secret." yyyy ".$oauth_verifier."<br>
//<br>
//<br>
//";
// Get the access token using HTTP GET and HMAC-SHA1 signature
$retarr = get_access_token(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
$request_token, $request_token_secret,
$oauth_verifier, false, true, true);
if (! empty($retarr)) {
list($info, $headers, $body, $body_parsed) = $retarr;
if ($info['http_code'] == 200 && !empty($body)) {
// print "Use oauth_token as the token for all of your API calls:\n" .
rfc3986_decode($body_parsed['oauth_token']);
}
}
/**
* Get an access token using a request token and OAuth Verifier.
* @param string $consumer_key obtained when you registered your app
* @param string $consumer_secret obtained when you registered your app
* @param string $request_token obtained from getreqtok
* @param string $request_token_secret obtained from getreqtok
* @param string $oauth_verifier obtained from step 3
* @param bool $usePost use HTTP POST instead of GET
* @param bool $useHmacSha1Sig use HMAC-SHA1 signature
* @param bool $passOAuthInHeader pass OAuth credentials in HTTP header
* @return array of response parameters or empty array on error
*/
function get_access_token($consumer_key, $consumer_secret, $request_token, $request_token_secret, $oauth_verifier, $usePost=false, $useHmacSha1Sig=true, $passOAuthInHeader=true)
{
$retarr = array(); // return value
$response = array();
$url = 'https://api.login.yahoo.com/oauth/v2/get_token';
$params['oauth_version'] = '1.0';
$params['oauth_nonce'] = mt_rand();
$params['oauth_timestamp'] = time();
$params['oauth_consumer_key'] = $consumer_key;
$params['oauth_token']= $request_token;
$params['oauth_verifier'] = $oauth_verifier;
// compute signature and add it to the params list
if ($useHmacSha1Sig) {
$params['oauth_signature_method'] = 'HMAC-SHA1';
$params['oauth_signature'] =
oauth_compute_hmac_sig($usePost? 'POST' : 'GET', $url, $params,
$consumer_secret, $request_token_secret);
} else {
$params['oauth_signature_method'] = 'PLAINTEXT';
$params['oauth_signature'] =
oauth_compute_plaintext_sig($consumer_secret, $request_token_secret);
}
// Pass OAuth credentials in a separate header or in the query string
if ($passOAuthInHeader) {
$query_parameter_string = oauth_http_build_query($params, false);
$header = build_oauth_header($params, "yahooapis.com");
$headers[] = $header;
} else {
$query_parameter_string = oauth_http_build_query($params);
}
// POST or GET the request
if ($usePost) {
$request_url = $url;
logit("getacctok:INFO:request_url:$request_url");
logit("getacctok:INFO:post_body:$query_parameter_string");
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$response = do_post($request_url, $query_parameter_string, 443, $headers);
} else {
$request_url = $url . ($query_parameter_string ?
('?' . $query_parameter_string) : '' );
logit("getacctok:INFO:request_url:$request_url");
$response = do_get($request_url, 443, $headers);
}
// extract successful response
if (! empty($response)) {
list($info, $header, $body) = $response;
$body_parsed = oauth_parse_str($body);
if (! empty($body_parsed)) {
logit("getacctok:INFO:response_body_parsed:");
//print_r($body_parsed);
}
$retarr = $response;
$retarr[] = $body_parsed;
}
return $retarr;
}
$guid = $retarr[3]["xoauth_yahoo_guid"];
$access_token = $retarr[3]["oauth_token"];
$access_token_secret = $retarr[3]["oauth_token_secret"];
// Call Contact API
$retarr = callcontact(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET,
$guid, $access_token, $access_token_secret,
false, true);
function callcontact($consumer_key, $consumer_secret, $guid, $access_token, $access_token_secret, $usePost=false, $passOAuthInHeader=true)
{
$retarr = array(); // return value
$response = array();
$url = 'http://social.yahooapis.com/v1/user/' . $guid . '/contacts;count=5';
$params['format'] = 'json';
$params['view'] = 'compact';
$params['oauth_version'] = '1.0';
$params['oauth_nonce'] = mt_rand();
$params['oauth_timestamp'] = time();
$params['oauth_consumer_key'] = $consumer_key;
$params['oauth_token'] = $access_token;
// compute hmac-sha1 signature and add it to the params list
$params['oauth_signature_method'] = 'HMAC-SHA1';
$params['oauth_signature'] =
oauth_compute_hmac_sig($usePost? 'POST' : 'GET', $url, $params,
$consumer_secret, $access_token_secret);
// Pass OAuth credentials in a separate header or in the query string
if ($passOAuthInHeader) {
$query_parameter_string = oauth_http_build_query($params, true);
$header = build_oauth_header($params, "yahooapis.com");
$headers[] = $header;
} else {
$query_parameter_string = oauth_http_build_query($params);
}
// POST or GET the request
if ($usePost) {
$request_url = $url;
logit("callcontact:INFO:request_url:$request_url");
logit("callcontact:INFO:post_body:$query_parameter_string");
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$response = do_post($request_url, $query_parameter_string, 80, $headers);
} else {
$request_url = $url . ($query_parameter_string ?
('?' . $query_parameter_string) : '' );
logit("callcontact:INFO:request_url:$request_url");
$response = do_get($request_url, 80, $headers);
}
// extract successful response
if (! empty($response)) {
list($info, $header, $body) = $response;
if ($body) {
logit("callcontact:INFO:response:");
print(json_pretty_print($body));
}
$retarr = $response;
}
return $retarr;
}
?>