good_evening
good_evening

Reputation: 21739

How to do a POST with XML API?

Here we see: https://developers.google.com/youtube/2.0/developers_guide_protocol_comments#Adding_a_comment

I need to do a request with XML API.

POST /feeds/api/videos/VIDEO_ID/comments HTTP/1.1
Host: gdata.youtube.com
Content-Type: application/atom+xml
Content-Length: CONTENT_LENGTH
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
    xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <content>This is a crazy video.</content>
</entry>

What should I use for this?

Upvotes: 1

Views: 542

Answers (2)

jlmcdonald
jlmcdonald

Reputation: 13667

You'll likely find it easiest to use one of the client libraries rather than do the POST manually, because it will handle header generation, authentication, and tokens for you without a lot of hassle. A list of the client libraries is here:

https://developers.google.com/youtube/code

For example, to do a comment post with the Python client, it would look something like this (assuming you'd gone through the authentication steps, which the clients make pretty simple):

my_comment = 'what a boring test video'
video_id = '9g6buYJTt_g'
video_entry = yt_service.GetYouTubeVideoEntry(video_id=video_id)
yt_service.AddComment(comment_text=my_comment, video_entry=video_entry)

The clients for other languages follow the same structure.

Upvotes: 1

Inglis Baderson
Inglis Baderson

Reputation: 799

You can use cURL to do this.

<?php
$data = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
    xmlns:yt="http://gdata.youtube.com/schemas/2007">
    <content>This is a crazy video.</content>
</entry>
XML;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'gdata.youtube.com/feeds/api/videos/VIDEO_ID/comments');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER,
    array('Content-Type: application/atom+xml',
    'Authorization: Bearer ACCESS_TOKEN',
    'GData-Version: 2',
    'X-GData-Key: key=DEVELOPER_KEY'));
$re = curl_exec($ch);
curl_close($ch);
?>

Upvotes: 1

Related Questions