0

我正在开发一个脚本来向 YouTube 用户发送消息。实际上,我已经获得了包含该通讯作者的 YouTube 视频 ID、作者姓名、详细信息等信息的 YouTube 数据提要。我必须使用 YouTube api 发送消息。这可能吗?

我已经编写了 Oauth 登录和 YouTube API 提要来获取相应视频的视频和用户信息。我在 YouTube API 中引用了此文档 - https://developers.google.com/youtube/2.0/developers_guide_protocol_messages#Sending_a_message用于发送消息,但没有此选项。只能选择将消息作为评论而不是新邮件发送到相应视频作者的收件箱。

所以,如果你知道的话,请更新我。我在下面粘贴了我的脚本。

 $developer_key='###########################';
    $client_id=     '#################';
    $client_secret='#################';

    // error checking; user might have denied access
    if (isset($_GET['error'])) {
        if ($_GET['error'] == 'access_denied') {
            echo('You have denied access. Click <a href="'. $_SERVER["SCRIPT_NAME"] .'">here</a> to retry&hellip;');
        } else {
            echo("An error has occurred: ". $_GET['error']);
        }
        exit;
    }

    // Step 1: redirect to google account login if necessary
    if(!isset($_GET['code']) || $_GET['code'] === '') { 
        Header('Location: https://accounts.google.com/o/oauth2/auth?client_id='. $client_id .
                '&redirect_uri=http%3A%2F%2Flocalhost%2Ftest%2Foauth_1.php' .
                '&scope=https://gdata.youtube.com&response_type=code&access_type=offline',
            true, 307);
        exit;
    }
    $authorization_code= $_GET['code'];

    // Step 2: use authorization code to get access token
    $url = "https://accounts.google.com/o/oauth2/token";
    $message_post= 'code='. $authorization_code .
            '&client_id='. $client_id . 
            '&client_secret='. $client_secret .
            '&redirect_uri=http://localhost/test/oauth_1.php' .
            '&grant_type=authorization_code';


    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $message_post);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);

    if ($cur_error= curl_error($ch)) {
        echo($cur_error);
        curl_close($ch);
        exit;
    }
    curl_close($ch);

    $jsonArray= json_decode($result, true);

    if ($jsonArray === null) {
        echo("Could not decode JSON.");
        exit;
    }

    if (isset($jsonArray['error'])) {
        echo("An error has occurred: ". $jsonArray['error']);
        exit;
    }

    if (!isset($jsonArray['access_token'])) {
        echo("Access token not found.");
        exit;
    }


    //The user's authentication token
     $access_token= $jsonArray['access_token'];
    $title ='krishna'; //The title of the caption track
    $lang = 'en'; //The languageof the caption track
    //$transcript = $_REQUEST['transcript']; //The caption file data
    $ur='https://gdata.youtube.com/feeds/api/users/recepient_username/inbox';



    $headers = array(

         'Host: gdata.youtube.com',
        'Content-Type: application/atom+xml utf-8',
        'Content-Language: ' . $lang,
        'Slug: ' . rawurlencode($title),
        'Authorization: AuthSub token=' . $access_token,
        'GData-Version: 2',
        'X-GData-Key: key=' . $developer_key
    );


    $xml = '&xml=<?xml version="1.0" encoding="UTF-8"?>
    <entry xmlns="http://www.w3.org/2005/Atom"
        xmlns:yt="http://gdata.youtube.com/schemas/2007">
      <id>cSVSeHFKBjU</id>
      <summary>sending a message from the api</summary>
    </entry>';
    // create a new cURL resource
    $ch = curl_init();

    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_HEADER, TRUE );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers );
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($xml) );
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_VERBOSE, 1 );

    $tt = curl_getinfo($ch);
    print_r($tt);


    $result = curl_exec($ch);
    print_r($result);


    // close cURL resource, and free up system resources
    curl_close($ch);

echo "DONE! Token:" . $access_token . "<br />\n";
var_dump($result);

因此,如果有任何选项可以向视频用户发送邮件而不是评论,请检查并更新我。

4

1 回答 1

0

YouTube v2.0 API 确实支持向用户的收件箱发送新消息,但该消息必须同时包含视频和关于该视频的评论。您不能发送纯文本消息。

可以在此处找到文档,其中包括一个解释如何使用 YouTube 的消息传递支持的用例。

于 2013-04-30T14:16:22.867 回答