尝试使用phpSmug访问根节点的未列出的子节点

时间:2017-11-15 23:10:14

标签: smugmug

我已修改phpSmug附带的example-oauth.php文件以列出根节点的直接子节点,但仅列出公用文件夹(包含至少一个公用相册的公用文件夹)和公用相册。我知道我的授权(Access=Full)是正确的,因为当我尝试列出所有用户相册时,我确实得到了所有这些(代码未显示)。当我在SmugMug站点上使用实例时,我确实得到了根节点的所有直接子节点。所以,似乎问题出在phpSmug中,而不是SmugMug API本身。此外,我正在更换一个同样有效的Chrome应用程序。 (当然,Chrome应用程序不使用phpSmug。)

知道什么是错的以及我如何解决它?

这是我修改过的例子。我添加或更改的行标有首字母MJR。再次注意,我没有写过这个程序的大部分内容,但只添加了几行。

/* Last updated with phpSmug 4.0
 *
 * This example file shows you how to authenticate using OAuth and then display
 * the first 25 images in the first public gallery found of the authenticated
 * user's account.
 *
 * You'll want to set the following variables below:
 *
 * - $APIKey with one provided by SmugMug: http://www.smugmug.com/hack/apikeys
 * - $OAuthSecret with one provided when you obtained your API key
 * - $AppName with your application name, version and URL, eg
 *
 * The $AppName is NOT required, but it's encouraged as it will allow SmugMug to
 * diagnose any issues users may have with your application if they request help
 * on the SmugMug forums. A good format to use is "APP NAME/VER (URL)".
 *
 */

$APIKey = 'YOUR_API_KEY';  // MJR -- my own data was entered
$OAuthSecret = 'YOUR_OAUTH_SECRET';  // MJR -- my own data was entered
$AppName = 'YOUR_APP_NAME/VER (URL)';
?>
<html>
<head>
    <title>phpSmug OAuth Login Example</title>
    <style type="text/css">
        body { background-color: #fff; color: #444; font-family: sans-serif }
        div { width: 750px; margin: 0 auto; text-align: center; }
        img { border: 0;}
    </style>
</head>
<body>
    <div>
        <a href="http://phpsmug.com"><img src="phpSmug-logo.svg" /></a>
        <h1>OAuth Login Example</h1>
<?php

try {
    $options = [
        'AppName' => $AppName,
        '_verbosity' => 1, # Reduce verbosity to reduce the amount of data in the response and to make using it easier.
        'OAuthSecret' => $OAuthSecret, # You need to pass your OAuthSecret in order to authenticate with OAuth.
        'verify' => false // MJR: Added because SSL certificates are not set up
    ];

    $client = new phpSmug\Client($APIKey, $options);

    // Perform the 3 step OAuth Authorisation process.
    // NOTE: This is a very simplified example that does NOT store the final token.
    // You will need to ensure your application does.
    if (!isset($_SESSION['SmugGalReqToken'])) {

        // Step 1: Get a request token using an optional callback URL back to ourselves
        $callback = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['SCRIPT_NAME'];
        $request_token = $client->getRequestToken($callback);
        $_SESSION['SmugGalReqToken'] = serialize($request_token);

        // Step 2: Get the User to login to SmugMug and authorise this demo
        echo '<p>Click <a href="'.$client->getAuthorizeURL(['Access' => 'Full']).'"><strong>HERE</strong></a> to Authorize This Demo.</p>'; // MJR
        // Alternatively, automatically direct your visitor by commenting out the above line in favour of this:
        //header("Location:".$client->getAuthorizeURL());
    } else {
        $reqToken = unserialize($_SESSION['SmugGalReqToken']);
        unset($_SESSION['SmugGalReqToken']);

        // Step 3: Use the Request token obtained in step 1 to get an access token
        $client->setToken($reqToken['oauth_token'], $reqToken['oauth_token_secret']);
        $oauth_verifier = $_GET['oauth_verifier'];  // This comes back with the callback request.
        $token = $client->getAccessToken($oauth_verifier);  // The results of this call is what your application needs to store.
        // Get the username of the authenticated user
        $username = $client->get('!authuser')->User->NickName;

        // List top level folders and albums.  // MJR
        $user = $client->get('!authuser'); // MJR
        $root = $client->get($user->User->Uris->Node); // MJR
        $children = $client->get($root->Node->Uris->ChildNodes); // MJR
        foreach ($children->Node as $v) { // MJR
            echo "<br> $v->Type \"$v->Name\" $v->Uri"; // MJR
        } // MJR

    }
} catch (Exception $e) {
    echo "{$e->getMessage()} (Error Code: {$e->getCode()})";
}
?>
    </div>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

您的更改应该有效,并且适合我。我必须做的一件事就是减少返回的节点数量,因为ChildNodes默认只返回前10个节点,而我的第一张私人专辑就在最后。

你的第一张私人专辑/文件夹是否在前10张之外?如果是,请尝试将更改更改为:

// List top level folders and albums.  // MJR
$user = $client->get('!authuser'); // MJR
$root = $client->get($user->User->Uris->Node); // MJR
$children = $client->get($root->Node->Uris->ChildNodes, ['count' => 50]); // MJR
foreach ($children->Node as $v) { // MJR
    echo "<br> $v->Type \"$v->Name\" $v->Uri"; // MJR
} // MJR

您会看到我已将['count' => 50]添加到选项中。如果您有超过50张专辑/文件夹,则可能需要增加此值。

或者,您可以使用$children->Pages中的信息迭代页面。

答案 1 :(得分:0)

解决!我使用的是几年前我获得的API密钥。我创建了一个新的API密钥,它可以工作!在SmugMug进入API 2之前,必须留下一些奇怪的状态。

仅供参考:在返回的JSON中,ResponseLevel现在设置为Full。如果将来有其他人出现,并且您确定ResponseLevel设置为Public,那么这可能是他们的问题与我的问题相似的线索。

感谢您的帮助,lildude。你在我的书中是个大家伙!