如何使用目标c中的gdata将视频上传到收藏/播放列表

时间:2010-10-28 06:57:50

标签: iphone objective-c gdata-api

我正在尝试将视频上传到我的帐户中的收藏夹但显示无效请求Uri 且状态代码= 400

我不明白我应该如何格式化我的请求

我的代码

    NSURL *url = [NSURL URLWithString:
                 http://gdata.youtube.com/feeds/api/users/username/favorite];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request setPostValue:@"gdata.youtube.com" forKey:@"Host"];
[request setPostValue:@"application/atom+xml" forKey:@"Content-Type"];
[request setPostValue:@"CONTENT_LENGTH" forKey:@"Content-Length"];
[request setPostValue:@"" forKey:@"AuthSubToken"];
[request setPostValue:@"2" forKey:@"GData-Version"];
[request setPostValue:developer_key forKey:@"X-GData-Key"];
[request setPostValue:xml_data forKey:@"API_XML_Request"];

[request setDelegate:self];
[request setDidFailSelector:@selector(requestFailed:)];
[request setDidFinishSelector:@selector(gotTheResponse:)];
[[networkQueue go];

我有auth令牌和开发人员密钥,VIDEO_ID。但我不知道如何在post请求中传递xml数据:

<?xml version="1.0" encoding="UTF-8"?>
 <entry xmlns="http://www.w3.org/2005/Atom">
  <id>VIDEO_ID</id>
 </entry>

NSString * xml_data =包含字符串形式的xml数据

2 个答案:

答案 0 :(得分:2)

NSString *developer_key;

GDataEntryYouTubeFavorite *favorite = [GDataEntryYouTubeFavorite entry];

[favorite setIdentifier:video_id];

service = [[GDataServiceGoogleYouTube alloc] init];

[service setUserCredentialsWithUsername:user_name password:user_pass];

[service setYouTubeDeveloperKey:developer_key];

NSURL *feedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:@"default" 
                                 userFeedID:kGDataYouTubeUserFeedIDFavorites];

[service fetchEntryByInsertingEntry:favorite forFeedURL:feedURL 
                           delegate:self 
                didFinishSelector:@selector(subscribeTicket:finishedWithEntry:)];

答案 1 :(得分:0)

请求有很多问题:它缺少ClientLogin或OAuth身份验证令牌,没有Content-Length,XML缺少命名空间,可能还有其他问题。

您是否考虑过使用GData Objective-C client library?使用该库,添加收藏夹如下所示:

  NSURL *favoritesFeedURL = [GDataServiceGoogleYouTube youTubeURLForUserID:username
                                                                userFeedID:kGDataYouTubeUserFeedIDFavorites];
  GDataServiceGoogleYouTube *service = [self youTubeService];
  [service fetchFeedWithURL:favoritesFeedURL
          completionHandler:^(GDataServiceTicket *ticket, GDataFeedBase *feed, NSError *error) {

            if (error == nil) {
              // add the new favorite
              GDataEntryYouTubeVideo *entry = [GDataEntryYouTubeVideo videoEntry];
              [entry setIdentifier:videoID];

              [service fetchEntryByInsertingEntry:entry
                                       forFeedURL:[[feed postLink] URL]
                                completionHandler:^(GDataServiceTicket *ticket, GDataEntryBase *entry, NSError *error) {

                                  if (error == nil) {
                                    // succeeded
                                  } else {
                                    NSLog(@"Error inserting new favorite: %@", error); 
                                  }
                                }];

            } else {
              NSLog(@"Error fetching favorites feed: %@", error); 
            }

          }];