2013-01-08 67 views
3

試圖讓我的應用使用SLRequest發佈到Facebook,但遇到問題。權限被批准罰款,它只是不會發布。在iOS 6中使用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。對此有何想法?

UPDATE:所以問題是HTTP響應回來爲400

+0

我有同樣的問題,但只有與特定的Facebook帳戶。如果您找到解決方案,請更新 – zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz

回答

2

,您應該已經複製/粘貼錯誤。

該選項已實例化兩次,以及您聯鎖的兩個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重新開始。

+0

第一個可以訪問在發佈到牆上之前所需的基本信息。這是第二個人做的。 – user717452

+0

我最終能夠檢索到這個錯誤:'必須使用一個活動的訪問令牌來查詢關於當前用戶的信息。「,」type「:」OAuthException「,」code「:2500}}' – user717452

+0

What * basic info *你在說什麼?你不需要額外的先決條件來使用'/ me/feed'查詢。你能解釋一下你認爲它的工作原理嗎? –

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

是的,您是對的即使您使用ACAccountStore在您的應用程序中登錄,只要您在Feed中發佈,您必須重新獲取憑證再次通過向ACAccountStore請求ACAccount。當然,如果您之前獲得了權限,則不會再次請求它們,因此對於用戶來說操作是透明的。 – viggio24

0

斯威夫特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))") 
    } 
} 
相關問題