将 iOS 应用永久许可 post 给 Facebook

Give iOS app permanent permission to post to Facebook

我已将使用 SLRequest 的 Facebook 共享集成到我的 iOS 应用程序中。一切正常,但每次我想要 post 时,都会向用户显示一条提示,询问是否应允许该应用代表他 post。

出于某些原因(我 post 使用 Apple Watch),获得 post 的永久许可很重要。否则会是非常糟糕的用户体验。

所以我正在寻找一种可能性,即代表用户为我的应用程序永久启用 posting。

- (void)postToFacebook:(NSString *)postMessage toAudience:(int)audienceIndex {

ACAccountStore *accountStore = [[ACAccountStore alloc] init];

ACAccountType *accountTypeFacebook =
[accountStore accountTypeWithAccountTypeIdentifier:
 ACAccountTypeIdentifierFacebook];

NSArray *audienceArray = [[NSArray alloc] initWithObjects:@"ACFacebookAudienceEveryone", @"ACFacebookAudienceFriends", @"ACFacebookAudienceOnlyMe", nil];

NSDictionary *options = @{ACFacebookAppIdKey: @"<HERE'S MY FACEBOOK APP ID>", ACFacebookPermissionsKey: @[@"publish_actions"], ACFacebookAudienceKey: [audienceArray objectAtIndex:audienceIndex]};

[accountStore requestAccessToAccountsWithType:accountTypeFacebook options:options completion:^(BOOL granted, NSError *error) {

    if(granted) {

        NSArray *accounts = [accountStore accountsWithAccountType:accountTypeFacebook];
        ACAccount *facebookAccount = [accounts lastObject];

        NSDictionary *parameters =
        @{@"access_token":facebookAccount.credential.oauthToken,
          @"message": postMessage};

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

        SLRequest *feedRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:feedURL parameters:parameters];

        [feedRequest  performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
            NSLog(@"Request failed, %@", [urlResponse description]);
        }];

    } else {

        NSLog(@"Access Denied");
        NSLog(@"[%@]",[error localizedDescription]);

    }
}];

}

不幸的是,不再有"permanent permission to post to Facebook"的概念。

offline_access 权限已弃用并于 2012 年 12 月 5 日删除(原定于 7 月 5 日)。

参见:Developer Docs > Migration > Remove offline_access Permission

它已被新的 60 天长期访问令牌所取代。

参见:Developer Docs > Facebook Login > Access Tokens

Short-Term and Long-Term Tokens

User access tokens come in two forms: short-lived tokens and long-lived tokens. Short-lived tokens usually have a lifetime of about an hour or two, while long-lived tokens usually have a lifetime of about 60 days. You should not depend on these lifetimes remaining the same - the lifetime may change without warning or expire early. See more under handling errors.

Access tokens generated via web login are short-lived tokens, but you can upgrade them to long-lived tokens. Converting short-lived tokens to long-lived tokens is covered later in this document under Expiration and Extending Tokens.

Mobile apps that use Facebook's mobile SDKs get long-lived tokens.

本质上,使用较新 SDK 的移动应用程序将自动授予用户 access_token 更长的有效期。只要用户在 60 天内再次访问您的应用,您就会被授予新用户 access_token,并具有新的到期时间。如果您的用户等待超过 60 天,他们将不得不重新登录。