2013-04-30 285 views
0

我正在開發一個腳本來向YouTube用戶發送消息。實際上,我已經獲得了YouTube數據提供的相關信息 - YouTube視頻ID,作者姓名,詳細信息等。我必須使用YouTube API發送消息。這可能嗎?如何使用youtube API將消息發送給youtube用戶?

我已經編寫了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); 

所以請檢查並更新我是否有任何選項向視頻用戶發送郵件而不是評論。

回答

0

YouTube v2.0 API確實支持將新郵件發送到用戶的收件箱,但該郵件必須同時包含有關該視頻的視頻和評論。您無法發送純文字信息。

文檔包括解釋如何使用YouTube的消息支持的用例,可以找到here