如何使用 Objective C 中的 gdata 将视频上传到收藏夹/播放列表

发布于 2024-09-29 02:42:17 字数 1192 浏览 7 评论 0原文

我正在尝试将视频上传到我帐户中的收藏夹,但它显示无效的请求 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];

我有身份验证令牌和开发人员密钥,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 = contains string form xml data

i am trying to upload a video to favorite in my account but it shows Invalid request Uri and status code =400

i dont understand how should i format my request

my code

    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];

i have auth token and developer key, VIDEO_ID.but m not sure how to pass xml data in post request:

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

NSString *xml_data = contains xml data in string form

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-10-06 02:42:17
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:)];
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:)];
幸福丶如此 2024-10-06 02:42:17

该请求存在很多错误:它缺少 ClientLogin 或 OAuth 身份验证令牌、没有内容长度、XML 缺少命名空间,以及可能的其他问题。

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

  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); 
            }

          }];

There is a lot wrong with the request: it lacks a ClientLogin or OAuth authentication token, there's no Content-Length, the XML lacks namespaces, and probably other issues.

Have you considered using the GData Objective-C client library? Using the library, adding a favorite looks like this:

  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); 
            }

          }];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文