我正在尝试在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);
}
}];
如果我对会话进行 NS 记录,我得到的是:
FBSessionStateOpenTokenExtended,loginHandler: 0x15eab870,appID: 719202928131376,urlSchemeSuffix:,tokenCachingStrategy:,expiration date:4001-01-01 00:00:00 0000,refresh date:2014-05-10 12:57:41 0000,attempted refresh date:0001-12-30 00:00
现在,如果我尝试使用以下方式发布图片:
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 */
}];
我收到错误消息:
错误域=com.facebook。sdk Code=5“操作无法完成。(com.facebook.sdk error 5.)”UserInfo=0x15e4f370{com.facebook.sdk:HTTPStatusCode=403,com.facefacebook.sdk:ParsedJSONResponseKey={body={error={Code=200;message=“(#200)Permissions error”;type=OAuthException;};};代码=403;},com.facebook。sdk:ErrorSessionKey=,过期日期:4001-01-01 00:00:00 0000,刷新日期:2014-05-10 12:57:41 0000,尝试刷新日期:0001-12-30 00:00:0 0000,权限:(状态,权限,“publish_actions”)
甚至,如果我再次获得权限时,“publish_actions”不在列表中。请指导我做错了什么。
有没有其他方式分享/发布带有描述的图片(没有任何链接,这是分享对话所必需的)?
在敲了几个小时的头之后,我想通了。似乎您需要进入“developers.facebook.com”状态“下的”应用程序设置”
这对我有用。
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);
}
}];
}