0

我真的很难找到一种方法来成功验证用户身份并将书签发布到他们的美味帐户。

我下载了Yahoo 的 YOS Social SDK,然后开始修改 oauth sampleapp.php。它基本上通过创建一个登录链接来处理 oAuth 过程,该链接将您发送到 Yahoo 以授予权限,然后将您发送回您指定的回调 URL。

我的想法是修改 sampleapp.php 文件以在oAuth 过程结束后向美味的帖子/添加 API发出请求以添加新书签,但我遇到了一些问题。我认为我以错误的方式处理 POST 请求。

这是我的代码:

<?php

require dirname(__FILE__).'/lib/Yahoo.inc';

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', true);
YahooLogger::setDebug(true);
YahooLogger::setDebugDestination('LOG');

ini_set('session.save_handler', 'files');
session_save_path('/tmp/');
session_start();

define('OAUTH_CONSUMER_KEY', '<YOURS_GOES_HERE>');
define('OAUTH_CONSUMER_SECRET', '<YOURS_GOES_HERE>');
define('OAUTH_DOMAIN', '<YOURS_GOES_HERE>');
define('OAUTH_APP_ID', '<YOURS_GOES_HERE>');

if(array_key_exists("logout", $_GET)) {
  YahooSession::clearSession();
  header("Location: sampleapp.php");
}

$hasSession = YahooSession::hasSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);

if($hasSession == FALSE) {

  $callback = YahooUtil::current_url();
  $auth_url = YahooSession::createAuthorizationUrl(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $callback);

}
else {

  $session = YahooSession::requireSession(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, OAUTH_APP_ID);

  if($session) {

    $consumer = new stdclass();
        $consumer->key = OAUTH_CONSUMER_KEY;
        $consumer->secret = OAUTH_CONSUMER_SECRET;

    $nativeSession = new NativeSessionStore();
    $token = $nativeSession->fetchAccessToken();

    $client = new OAuthClient($consumer, $token, OAUTH_PARAMS_IN_HEADERS, OAUTH_SIGNATURE_HMAC_SHA1);

    $request_url = 'http://api.del.icio.us/v2/posts/add';
    $bookmark_url = 'http://www.tegdesign.com';
    $parameters = array("url" => urlencode($bookmark_url), "description" => "test");

    $response = $client->post($request_url,'TEXT',$parameters);

    echo '<pre>';
    print_r($response);
    echo '</pre>';

  }

}


if($hasSession == FALSE) {

  echo sprintf("<a href=\"%s\" id=\"yloginLink\">Login</a>\n", $auth_url);

} else if($hasSession) {

  echo "<p><a href=\"?logout\">Logout</a></p>";

}

?>

这是 $response 变量的输出:

Array
(
    [method] => POST
    [url] => http://api.del.icio.us/v2/posts/add
    [code] => 401
    [requestHeaders] => Array
        (
            [0] => Accept: application/json
            [1] => Authorization: OAuth realm="yahooapis.com",oauth_version="1.0",oauth_nonce="<MINE_SHOWS_HERE>",oauth_timestamp="1289407587",oauth_consumer_key="<MINE_SHOWS_HERE>",oauth_token="<MINE_SHOWS_HERE>",oauth_signature_method="HMAC-SHA1",oauth_signature="<MINE_SHOWS_HERE>"
            [2] => Content-Type: TEXT
        )

    [requestBody] => Array
        (
            [url] => http%3A%2F%2Fwww.tegdesign.com
            [description] => test
        )

    [responseHeaders] => Array
        (
            [Date] => Wed, 10 Nov 2010 16:46:32 GMT
            [WWW-Authenticate] => OAuth oauth_problem="signature_invalid", realm="yahooapis.com"
            [Content-Type] => application/json
            [Cache-Control] => private
            [Age] => 0
            [Transfer-Encoding] => chunked
            [Connection] => keep-alive
            [Server] => YTS/1.17.21
        )

    [responseBody] => {"error":{"lang":"en-US","description":"Please provide valid credentials"}}
)

有人知道使用 Delicious 新 API 成功验证身份并代表用户发布书签吗?我过得很糟糕。

谢谢,泰根

4

1 回答 1

1

将您的内容类型设置为,"application/x-www-form-urlencoded"而不是'Text'在下一行。

$response = $client->post($request_url,'TEXT',$parameters);
于 2011-11-09T13:28:07.403 回答