使用iOS SDK在Facebook上发布描述图片

时间:2014-05-10 13:14:09

标签: ios facebook facebook-graph-api

我想在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,attemptsRefreshDate:0001-12-30 00:00:00 +0000,权限:(     状态,     允许,     " publish_actions" )>

现在,如果我尝试使用以下方式发布图片:

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 Code = 5"操作无法完成。 (com.facebook.sdk错误5。)" UserInfo = 0x15e4f370 {com.facebook.sdk:HTTPStatusCode = 403,com.facebook.sdk:ParsedJSONResponseKey = {     body = {         error = {             代码= 200;             message ="(#200)权限错误&#34 ;;             type = OAuthException;         };     };     code = 403; },com.facebook.sdk:ErrorSessionKey =,expirationDate:4001-01-01 00:00:00 +0000,refreshDate:2014-05-10 12:57:41 +0000,attemptsRefreshDate:0001-12-30 00: 00:00 +0000,权限:(     状态,     允许,     " publish_actions"     )>}

甚至,如果我再次获得权限," publish_actions"不在列表中。 请指导我做错了什么。

是否有其他方式与描述共享/发布图片(没有任何链接,这是共享对话所必需的)?

2 个答案:

答案 0 :(得分:1)

经过几个小时的敲打,我想通了。 您似乎需要进入developers.facebook.com上的“状态和审核”下的应用设置,并请求您的应用发布权限。 如果您只需要测试它,您可以转到Roles并添加您尝试以应用程序的开发人员身份登录的Facebook用户。

答案 1 :(得分:0)

这对我有用。

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

}