0

YouTube ユーザーにメッセージを送信するスクリプトを開発しています。実際、私は YouTube のデータ フィードとその情報 (YouTube ビデオ ID、作者名、詳細など) を入手しました。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 に答える