Youtube API对视频发表评论

时间:2016-11-11 06:21:14

标签: android ios youtube youtube-api youtube-data-api

我正在使用Youtube API来显示视频,现在我需要在YouTube视频上发表评论。这是适用于iOS的代码

NSString *urlStr = @"http://gdata.youtube.com/feeds/mobile/videos/W_KEuea8eIw/comments";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *xmlString = @"<?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/>";
[request setHTTPMethod: @"POST" ];
[request setValue:@"application/atom+xml" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[xmlString dataUsingEncoding:NSUTF8StringEncoding]];
[request setValue:@"2.1" forHTTPHeaderField:@"GData-Version"];
[request setValue:[NSString stringWithFormat:@"key=%@",kSampleDeveloperKey] forHTTPHeaderField:@"X-GData-Key"];
[request setValue:[NSString stringWithFormat:@"Bearer %@",self.auth.accessToken] forHTTPHeaderField:@"Authorization"];
NSURLResponse *response;
NSError *err;
NSData*returnData=[ NSURLConnection sendSynchronousRequest: request returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[returnData bytes]];
NSLog(@"responseData: %@", content);

现在我尝试使用HTTP Post请求在Android上发布评论,但无法在android中创建POST URL

https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&videoId=ffL25JVXbo0&key=AIzaSyAQSINj5dtoLdSNJA3cc6dkziFC2zbbEuk&snippet.topLevelComment.snippet.textOriginal=my%20comment

1 个答案:

答案 0 :(得分:0)

好吧,我尝试了,并且我在您提供的视频中成功发布了评论。

enter image description here

尝试使用此documentation试用部分,并检查视频中是否有评论。

请注意,将下拉按钮更改为最新的,以便在检查视频中的评论时查看最新评论。

enter image description here

此外,CommentThreads: insert的文档中说明您必须为这些属性指定值:

  • snippet.channelId

  • snippet.topLevelComment.snippet.textOriginal

因此,请尝试在您的请求中包含channelId。