使用图形 api 使用受众选择器将 Facebook 发布到墙上

Posted

技术标签:

【中文标题】使用图形 api 使用受众选择器将 Facebook 发布到墙上【英文标题】:Facebook post to wall with audience selector using graph api 【发布时间】:2016-02-15 15:31:41 【问题描述】:

我已经用 Objective-C 编写了这段代码

#import <FacebookSDK/FacebookSDK.h>

#import "CustomLoginViewController.h"
#import "AppDelegate.h"

@implementation CustomLoginViewController

- (IBAction)buttonTouched:(id)sender


    if (FBSession.activeSession.state == FBSessionStateOpen
        || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) 

        [FBSession.activeSession closeAndClearTokenInformation];

     else 

        [FBSession openActiveSessionWithPublishPermissions:@[@"publish_actions"] defaultAudience:FBSessionDefaultAudienceOnlyMe allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) 

            __block NSString *alertText;
            __block NSString *alertTitle;
            if (!error) 
                if ([FBSession.activeSession.permissions
                     indexOfObject:@"publish_actions"] == NSNotFound)
                    // Permission not granted, tell the user we will not publish
                    alertTitle = @"Permission not granted";
                    alertText = @"Your action will not be published to Facebook.";
                    [[[UIAlertView alloc] initWithTitle:alertTitle
                                                message:alertText
                                               delegate:self
                                      cancelButtonTitle:@"OK!"
                                      otherButtonTitles:nil] show];
                 else 
                    // Permission granted, publish the OG story
                    //                                                    [self publishStory];
                    NSLog(@"Publish permission granted !");
                    [self publishPOST];
                

             else 
                // There was an error, handle it
                // See https://developers.facebook.com/docs/ios/errors/
            


            // Retrieve the app delegate
            AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
            // Call the app delegate's sessionStateChanged:state:error method to handle session state changes
            [appDelegate sessionStateChanged:session state:status error:error];
        ];
    //else

- (IBAction)post:(id)sender 


-(void)publishPOST
    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   @"Description goes here.",@"description",
                                   @"www.google.com/image.jpg",@"picture",
                                   @"www.google.com/invites/username", @"link",
                                   @"Google",@"name",
                                   @"www.google.com",@"caption",
                                   nil];

    [FBRequestConnection startWithGraphpath:@"me/feed" parameters:params HTTPMethod:@"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
        if (!error) 
            NSLog(@"published !");
         else 
            NSLog(@"published Failed ??");
        
    ];



@end

但问题是我想用 FBSessionDefaultAudienceNone 发帖, FBSessionDefaultAudienceOnlyMe、FBSessionDefaultAudienceFriends 和 FBSessionDefaultAudienceEveryone 以编程方式。

我已经写了 FBSessionDefaultAudienceOnlyMe 以仅作为我发布,它对我不起作用。 另一方面,它作为朋友发布。 请任何人帮助我以编程方式仅以我和每个人的身份发布。 如果我错了,请指导我。

提前致谢!

【问题讨论】:

【参考方案1】:

你可以试试这个代码,这可能会对你有所帮助

- (void)postData:(NSString *)message url:(NSURL *)url thumbnail:(NSURL *)thumbnail audience:(SocialNetworkAudience)audience
        
    NSString* privacy;
    switch (audience) 
        case SocialNetworkAudiencePrivate:
            privacy = @"SELF";
            break;
        case SocialNetworkAudienceProtected:
            privacy = @"ALL_FRIENDS";
            break;
        case SocialNetworkAudiencePublic:
            privacy = @"EVERYONE";
            break;
    

    [FBRequestConnection
        startWithGraphPath:@"/me/feed"
        parameters:@
            @"description": message,
            @"link": url.absoluteString,
            @"picture": thumbnail.absoluteString,
            @"privacy": @"'value': 'SELF'"
        
        HTTPMethod:@"POST"
        completionHandler:^(FBRequestConnection *connection, id result, NSError *error) 
        
    ];

这将发布到您在 facebook 中的提要中。

对于其他受众,您必须在此行中获得隐私价值来代替“SELF”。

@"privacy": @"'value': 'SELF'" 

【讨论】:

以上是关于使用图形 api 使用受众选择器将 Facebook 发布到墙上的主要内容,如果未能解决你的问题,请参考以下文章

如何使用图像选择器将照片发送到我的 hp 彩色打印机?

如何使用 API v3 从 mailchimp 受众获取所有标签?

Facebook Audience API - 使用 Shapefile 定位受众

通过包含/排除多个 pagePaths 的 API 批量创建 AdWords 受众

如何修复“访问令牌来自错误的受众或资源。”尝试使用 MSAL 令牌访问 Azure 的 REST api 时

如何在不使用会话的情况下获取图形令牌?