2

我想使用 dailymotion api 来获取我自己的私人视频的信息。所以...我有一个 Dailymotion 帐户我创建了一个 API 密钥和密钥我下载了 PHP 类我想获取我的私人视频的信息以在我的网站上显示它...

所以我想我需要验证我的帐户并在获取代码后......但它不起作用:'(请你给我一个示例代码来做到这一点吗?

我的测试代码现在就是这样

<?php 
error_reporting(E_ALL & ~E_NOTICE);
ini_set('display_errors', 1);

$apiKey = 'xxxx';
$apiSecret = 'xxxx';
require_once 'Dailymotion.php';
// Instanciate the PHP SDK.
$api = new Dailymotion();

// Tell the SDK what kind of authentication you'd like to use.
// Because the SDK works with lazy authentication, no request is performed at this point.
$api->setGrantType(Dailymotion::GRANT_TYPE_AUTHORIZATION, $apiKey, $apiSecret);

$api = new Dailymotion();
try
{
$result = $api->get(
'/video/privateVideoId',
array('fields' => array('id', 'title', 'owner'))
);

}
catch (DailymotionAuthRequiredException $e)
{
echo $e->getMessage();
// If the SDK doesn't have any access token stored in memory, it tries to
// redirect the user to the Dailymotion authorization page for authentication.
//return header('Location: ' . $api->getAuthorizationUrl());
}
catch (DailymotionAuthRefusedException $e)
{
echo $e->getMessage();
// Handle the situation when the user refused to authorize and came back here.
// <YOUR CODE>
}

trace($result);

function trace($d) {
echo '<pre>';
var_dump($d);
echo '</pre>';
}
?>

结果是:不允许该用户访问该视频。

所以我认为身份验证存在问题......但我不明白如何仅使用 php 来做到这一点

非常感谢你的帮助

4

1 回答 1

0

您的代码和身份验证方式似乎存在一些问题:

1)您的代码:您调用$api = new Dailymotion(); 然后使用您的 api 密钥和秘密设置授权授予类型。但是下一行,你通过重写来覆盖所有这些$api = new Dailymotion();。所以我建议你删除这一行,否则就像你没有设置任何授权类型!

2)有一个有趣的代码示例关于 php 中的授权授予类型,完全按照您正在尝试做的事情,在https://developer.dailymotion.com/tools/sdks#sdk-php-grant-authorization 您的代码是非常相似,为什么你return header('Location: ' . $api->getAuthorizationUrl()); 在捕捉时评论了这个部分DailymotionAuthRequiredException?这部分将用户重定向到身份验证页面,以便他/她可以进行身份​​验证。我还建议查看其他授权类型以进行身份​​验证,例如密码授权类型(https://developer.dailymotion.com/tools/sdks#sdk-php-grant-password

于 2015-06-08T15:00:11.870 回答