Reputation: 111
Trying to get my app to make a simple post to Facebook using SLRequest, but running into issues. Permissions get granted fine, it just won't post.
-(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);
}];
}
Error in log comes back null for the final part. Any thoughts on this?
UPDATE: So the issue is HTTP Response is coming back as 400.
Upvotes: 3
Views: 5151
Reputation: 1287
Swift 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))")
}
}
Upvotes: 0
Reputation: 164
Maybe a bit late... however, it looks like you have to make your feedRequest into the block where you have requested the publish permissions. Something like this :
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");
}
}];
Upvotes: 2
Reputation: 22903
You should have made a copy/paste error.
The options have been instanciated two times as well as the two requestAccessToAccountsWithType
method that you interlocked.
This looks very recurrent and improbable:
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 {
...
}
}];
You should start again from the beginning.
Upvotes: 2