【问题标题】:Using SLRequest in iOS 6 to Post to Facebook在 iOS 6 中使用 SLRequest 发布到 Facebook
【发布时间】:2017-08-04 21:42:54
【问题描述】:

试图让我的应用使用 SLRequest 向 Facebook 发布一个简单的帖子,但遇到了问题。权限被授予很好,它只是不会发布。

-(void)requestPermissions
{
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    __block ACAccount *facebookAccount = nil;

    ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    // Specify App ID and permissions
    NSDictionary *options = @{
ACFacebookAppIdKey: @"MYAPPID",
ACFacebookPermissionsKey: @[@"email", @"user_about_me", @"user_likes"],
ACFacebookAudienceKey: ACFacebookAudienceFriends
    };

    [accountStore requestAccessToAccountsWithType:facebookAccountType
                                          options:options completion:^(BOOL granted, NSError *e)
     {
         if (granted) {

             NSDictionary *options2 = @{
         ACFacebookAppIdKey: @"MYAPPID",
         ACFacebookPermissionsKey: @[@"publish_stream", @"publish_actions"],
         ACFacebookAudienceKey: ACFacebookAudienceFriends
             };
             [accountStore requestAccessToAccountsWithType:facebookAccountType options:options2 completion:^(BOOL granted, NSError *error) {
                 if (granted) {
                     NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];

                     facebookAccount = [accounts lastObject];                 }
                 else {
                     NSLog(@"Access denied 2");
                     NSLog(@"%@", [error description]);
                 }
             }];
         } else {
             NSLog(@"Error: %@", [e description]);
             NSLog(@"Access denied");
         }
     }];

    NSDictionary *parameters = @{@"message": @"This is a test"};

    NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

    SLRequest *feedRequest = [SLRequest
                              requestForServiceType:SLServiceTypeFacebook
                              requestMethod:SLRequestMethodPOST
                              URL:feedURL
                              parameters:parameters];
    NSLog(@"AnythingHere?");
    feedRequest.account = facebookAccount;

    [feedRequest performRequestWithHandler:^(NSData *responseData,
                                             NSHTTPURLResponse *urlResponse, NSError *error)
     {
         // Handle response
         NSLog(@"%@%@%@", error, responseData, urlResponse);
     }];
}

日志中的错误在最后部分返回 null。对此有什么想法吗?

更新:所以问题是 HTTP 响应返回为 400。

【问题讨论】:

  • 我遇到了同样的问题,但仅限于特定的 Facebook 帐户。如果您找到解决方案,请更新

标签: ios facebook social-framework


【解决方案1】:

您应该犯了复制/粘贴错误。

选项已经实例化了两次,以及您联锁的两个requestAccessToAccountsWithType 方法。

这看起来非常频繁且不太可能:

NSDictionary *options = @{...};
[accountStore requestAccessToAccountsWithType:facebookAccountType options:options completion:^(BOOL granted, NSError *e) {
    if (granted) {
         NSDictionary *options2 = @{...};
         [accountStore requestAccessToAccountsWithType:facebookAccountType options:options2 completion:^(BOOL granted, NSError *error) {
             if (granted)
                 ...
             else
                ...
        }];
    } else {
        ...
    }
}];

你应该从the beginning重新开始。

【讨论】:

  • 第一个提供访问基本信息的权限,这是在访问墙上发布之前所需的。这就是第二个所做的。
  • 我终于可以找回这个错误了:An active access token must be used to query information about the current user.","type":"OAuthException","code":2500}}
  • 您在说什么基本信息?使用 /me/feed 查询不需要额外的先决条件。你能解释一下你认为它是如何工作的吗?
  • 如果我只是请求 publish_stream,我会收到以下消息:Facebook 服务器无法完成此访问请求:应用程序必须在安装时请求基本读取权限。
【解决方案2】:

可能有点晚了...但是,您似乎必须将 feedRequest 放入您已请求发布权限的块中。像这样:

ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    __block ACAccount *facebookAccount = nil;

    ACAccountType *facebookAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];

    // Specify App ID and permissions
    NSDictionary *options = @{
                              ACFacebookAppIdKey: @"yourAppId",

                              ACFacebookPermissionsKey: @[@"email"]
                              ACFacebookAudienceKey: ACFacebookAudienceEveryone
                              }; // basic read permissions

    [accountStore requestAccessToAccountsWithType:facebookAccountType
                                          options:options completion:^(BOOL granted, NSError *e)
     {
         if (granted) {
             // Now that you have publish permissions execute the request 
             NSDictionary *options2 = @{
                                        ACFacebookAppIdKey: @"yourAppId",
                                        ACFacebookPermissionsKey: @[@"publish_stream", @"publish_actions"],
                                        ACFacebookAudienceKey: ACFacebookAudienceFriends
                                        };
             [accountStore requestAccessToAccountsWithType:facebookAccountType options:options2 completion:^(BOOL granted, NSError *error) {
                 if (granted) {
                     NSArray *accounts = [accountStore accountsWithAccountType:facebookAccountType];

                     facebookAccount = [accounts lastObject];

                     NSDictionary *parameters = @{@"message": @"This is a test"};

                     NSURL *feedURL = [NSURL URLWithString:@"https://graph.facebook.com/me/feed"];

                     SLRequest *feedRequest = [SLRequest
                                               requestForServiceType:SLServiceTypeFacebook
                                               requestMethod:SLRequestMethodPOST
                                               URL:feedURL
                                               parameters:parameters];
                     NSLog(@"AnythingHere?");

                     [feedRequest setAccount:facebookAccount];

                     [feedRequest performRequestWithHandler:^(NSData *responseData,
                                                              NSHTTPURLResponse *urlResponse, NSError *error)
                      {
                          // Handle response
                          NSLog(@"%@%@", error,urlResponse);

                          NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];

                          NSLog(@"Facebook Response : %@",response);

                      }];



                 }
                 else {
                     NSLog(@"Access denied 2");
                     NSLog(@"%@", [error description]);
                 }
             }];
         } else {
             NSLog(@"Error: %@", [e description]);
             NSLog(@"Access denied");
         }
     }];

【讨论】:

  • 是的,你是对的。即使您使用 ACAccountStore 登录您的应用程序,一旦您在提要中发布,您必须通过向 ACAccountStore 请求 ACAccount 再次重新获取凭据。当然,如果您之前获得了权限,则不会再次请求它们,因此对于用户而言,操作是透明的。
【解决方案3】:

斯威夫特 3

let account = ACAccountStore()
let accountType = account.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierFacebook)
let options: [AnyHashable : Any] = [ACFacebookAppIdKey: "Your App ID on FB", ACFacebookPermissionsKey: ["publish_stream", "publish_actions"], ACFacebookAudienceKey: ACFacebookAudienceEveryone]

account.requestAccessToAccounts(with: accountType, options: options) { (success, error)  in
  if success {
    if let accounts = account.accounts(with: accountType) {
      if accounts.isEmpty {
        print("No facebook account found, please add your facebook account in phone settings")
      } else {
        let facebookAccount = accounts.first as! ACAccount

        let message = ["status": "My first Facebook posting "]
        let requestURL = URL(string: "https://graph.facebook.com/me/feed")

        let postRequest = SLRequest(forServiceType: SLServiceTypeFacebook,
                                    requestMethod: SLRequestMethod.POST,
                                    url: requestURL,
                                    parameters: message)
        postRequest?.account = facebookAccount

        postRequest?.perform(handler: {(_, urlResponse,
          error) in

          if let err = error {
            print("Error : \(err.localizedDescription)")
          }
          print("Facebook HTTP response \(String(describing: urlResponse?.statusCode))")
        })
      }
    }
  } else {
    print("Facebook account error: \(String(describing: error))")
  }
}

【讨论】:

    猜你喜欢
    • 2012-09-16
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 2012-11-30
    • 2011-11-21
    相关资源
    最近更新 更多