簡體   English   中英

使用iOS SDK在Facebook上發布帶有說明的圖片

[英]Posting picture on Facebook with description using iOS SDK

我正在嘗試在Facebook上發布/分享圖片。 首先,我使用以下命令獲取發布權限:

        NSArray *permissionsNeeded = @[@"publish_actions"];
        [FBRequestConnection startWithGraphPath:@"/me/permissions"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                  if (!error){
                                      NSDictionary *currentPermissions= [(NSArray *)[result data] objectAtIndex:0];
                                      NSMutableArray *requestPermissions = [[NSMutableArray alloc] initWithArray:@[]];

                                      for (NSString *permission in permissionsNeeded){
                                          if (![currentPermissions objectForKey:permission]){
                                              [requestPermissions addObject:permission];
                                          }
                                      }

                                      if ([requestPermissions count] > 0){
                                          [FBSession.activeSession requestNewPublishPermissions:requestPermissions
                                                                                defaultAudience:FBSessionDefaultAudienceFriends
                                                                              completionHandler:^(FBSession *session, NSError *error) {
                                                                                  if (!error) {
                                                                                      [self shareDataOnFacebook];
                                                                                  } else {
                                                                                      NSLog(@"%@", error.description);
                                                                                  }
                                                                              }];
                                      } else {
                                          [self shareDataOnFacebook];
                                      }

                                  } else {
                                      NSLog(@"%@", error.description);
                                  }
                              }];

如果我NSLog會話,我得到這個:

FBSessionStateOpenTokenExtended,loginHandler:0x15eab870,appID:719202928131376,urlSchemeSuffix:,tokenCachingStrategy :, expirationDate:4001-01-01 00:00:00 +0000,refreshDate:2014-05-10 12:57:41 +0000,trypedRefreshDate:0001- 12-30 00:00:00 +0000,權限:(狀態,權限,“發布動作”)>

現在,如果我嘗試使用以下方式發布圖片:

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                       @"{image-url}", @"url",
                       nil
                   ];
/* make the API call */
[FBRequestConnection startWithGraphPath:@"/me/photos"
                         parameters:params
                         HTTPMethod:@"POST"
                  completionHandler:^(
                      FBRequestConnection *connection,
                      id result,
                      NSError *error
                  ) {
                      /* handle the result */
                  }];

我收到錯誤消息:

錯誤Domain = com.facebook.sdk代碼= 5“操作無法完成。(com.facebook.sdk錯誤5。)” UserInfo = 0x15e4f370 {com.facebook.sdk:HTTPStatusCode = 403,com.facebook.sdk :ParsedJSONResponseKey = {正文= {錯誤= {代碼= 200; message =“(#200)權限錯誤”; 類型= OAuthException; }; }; 代碼= 403; },com.facebook.sdk:ErrorSessionKey =,到期日期:4001-01-01 00:00:00 +0000,刷新日期:2014-05-10 12:57:41 + 0000,trypedRefreshDate:0001-12-30 00: 00:00 +0000,權限:(狀態,權限,“發布動作”)>}

即使,當我再次獲得許可時,“ publish_actions”也不在列表中。 請指導我我在做什么錯。

還有其他共享/發布帶有描述的圖片的方法(沒有任何鏈接,這是共享對話框所必需的)?

打了幾個小時后,我發現了。 看來您需要進入developer.facebook.com上“狀態和評論”下的“應用程序設置”,並請求應用程序的發布權限。 如果您只需要測試,則可以轉到“角色”並添加您要作為應用程序開發人員登錄的Facebook用戶。

這對我有用。

NSArray *requestPermission = @[@"publish_actions"];
        // Open session with public_profile
        [FBSession openActiveSessionWithPublishPermissions:requestPermission defaultAudience:FBSessionDefaultAudienceEveryone allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error) {
             if (!error){
                 // If the session was opened successfully
                 if (state == FBSessionStateOpen){
                     // Your code here
                      NSLog(@"session opened");
                     [self postToFacebook];

                 } else {
                     // There was an error, handle it
                     NSLog(@" error on opening session = %@",error);
                 }
             }
         }];





-(void)postToFacebook{
NSLog(@"post to facebook");
// Put together the dialog parameters
NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"Sharing Tutorial", @"name",
                               @"Build great social apps and get more installs.", @"caption",
                               @"Allow your users to share stories on Facebook from your app using the iOS SDK.", @"description",
                               @"https://developers.facebook.com/docs/ios/share/", @"link",
                               @"http://i.imgur.com/g3Qc1HN.png", @"picture",
                               nil];

// Make the request
[FBRequestConnection startWithGraphPath:@"/me/feed"
                             parameters:params
                             HTTPMethod:@"POST"
                      completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                          if (!error) {
                              // Link posted successfully to Facebook
                              NSLog(@"result: %@", result);
                          } else {
                              // An error occurred, we need to handle the error
                              // See: https://developers.facebook.com/docs/ios/errors
                              NSLog(@"%@", error.description);
                          }
                      }];

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM