Facebook Graph Api Fan页面照片上传(IOS) [英] Facebook Graph Api Fan page photo upload (IOS)

查看:231
本文介绍了Facebook Graph Api Fan页面照片上传(IOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序即时上传照片,我正在尝试添加上传到用户的Facebook粉丝页面(他们管理)的功能。



我遇到的问题是,我可以将照片成功上传到粉丝专页,但没有显示在粉丝专页时间轴上,仅显示在粉丝专页的他人最近的帖子部分。



我显然没有正确上传这些图片。这是我的代码:

  ACAccountType * accountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook]; 
[_accountStore requestAccessToAccountsWithType:accountType选项:@ {ACFacebookAppIdKey:@XXXXXXXXXXXX,ACFacebookPermissionsKey:@ [@photo_upload,@publish_stream,@publish_actions,@manage_pages],ACFacebookAudienceKey:ACFacebookAudienceEveryone}完成:^(BOOL grant,NSError * error){
if(approved){

NSArray * accounts = [_accountStore accountsWithAccountType:accountType];

NSDictionary * fanPages = [[NSUserDefaults standardUserDefaults] objectForKey:FACEBOOK_PREF_FAN_PAGES_IDS];

//如果使用粉丝页
if(fanPages.count)
{

for(id key in fanPages){
/ / id是密钥,访问令牌是
NSLog(@访问令牌:%@,fanpage id:%@,fanPages [key],key);

NSMutableString * requestURL = [NSMutableString stringWithFormat:@https://graph.facebook.com/%@/photos/,key];

NSURL * url = [NSURL URLWithString:requestURL];

NSDictionary * parameters = @ {@access_token:[fanPages [key] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],@no_story:@0,@message:hashtags};

SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:url parameters:parameters];

[request addMultipartData:UIImageJPEGRepresentation(image,0.9)withName:@sourcetype:@application / octet-streamfilename:@media.png];
// NSLog(@%@,[accounts lastObject]);
[request setAccount:[accounts lastObject]];

// make request
[request performRequestWithHandler:^(NSData * responseData,NSHTTPURLResponse * urlResponse,NSError * error){
NSLog(@upload for%@,fanPages [键]);

NSString * respData = [[NSString alloc] initWithData:responseData
encoding:NSUTF8StringEncoding];

NSLog(@responseData%@,respData);
NSLog(@urlResponse for%@,urlResponse);


[self performSelectorOnMainThread:@selector(removeProgressView)withObject:nil waitUntilDone:NO];

}];

}



}
else
{
//上传到个人时间轴
NSURL * url = [NSURL URLWithString:@https://graph.facebook.com/me/photos];
SLRequest * request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:url parameters:nil];
[request addMultipartData:[hashtags dataUsingEncoding:NSUTF8StringEncoding] withName:@messagetype:@multipart / form-datafilename:nil];
[request addMultipartData:UIImageJPEGRepresentation(image,0.9)withName:@sourcetype:@application / octet-streamfilename:@media.png];
[request setAccount:[accounts lastObject]];
[request performRequestWithHandler:^(NSData * responseData,NSHTTPURLResponse * urlResponse,NSError * error){
[self performSelectorOnMainThread:@selector(removeProgressView)withObject:nil waitUntilDone:NO];
}];
}

} else {

//这是一个错误,或者他们没有被授予

}

任何想法我在做错什么?我已经在互联网上洗了几天,一切都似乎正确。

解决方案

所以原来我在做一切正确的,问题是如果我进去:

  [请求setAccount:[accounts lastObject]]; 

这使得它代表我发布,不会显示在时间轴上。删除此代码解决了这个问题,我的所有帖子现在都显示在粉丝页面的时间轴上,而不是从我的Facebook用户帐户。



感谢Tobi,我知道这是访问令牌,我以为我正确传递,显然我没有。



所以只是重新迭代,不要在发布到粉丝页面时使用setAccount,因为它将使用用户令牌覆盖access_token,而不是粉丝页面令牌。


I have an app that uploaded photos on the fly and i'm trying to add the ability to upload to the users facebook fan page (that they administer).

The problem that i'm having is that i can sucessfully upload a photo to the fan page but it's not showing up on the fan page timeline, it's only showing up under the "Recent Posts by Others" section of the fan page.

I'm obviously not uploading these images properly. Here's my code:

ACAccountType * accountType = [_accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook];
        [_accountStore requestAccessToAccountsWithType:accountType options:@{ACFacebookAppIdKey: @"XXXXXXXXXXXX", ACFacebookPermissionsKey : @[@"photo_upload",@"publish_stream", @"publish_actions", @"manage_pages"], ACFacebookAudienceKey : ACFacebookAudienceEveryone} completion:^(BOOL granted, NSError *error) {
            if (granted) {

                NSArray * accounts = [_accountStore accountsWithAccountType:accountType];

                NSDictionary *fanPages = [[NSUserDefaults standardUserDefaults] objectForKey:FACEBOOK_PREF_FAN_PAGES_IDS];

                //if using fan pages
                if (fanPages.count)
                {

                    for (id key in fanPages) {
                        //id is the key and the access token is the value
                        NSLog(@"access token:%@, fanpage id: %@", fanPages[key], key);

                        NSMutableString *requestURL = [NSMutableString stringWithFormat:@"https://graph.facebook.com/%@/photos/", key];

                        NSURL * url = [NSURL URLWithString:requestURL];

                        NSDictionary *parameters = @{@"access_token": [fanPages[key] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], @"no_story":@"0", @"message": hashtags};

                        SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:url parameters:parameters];

                        [request addMultipartData:UIImageJPEGRepresentation(image, 0.9) withName:@"source" type:@"application/octet-stream" filename:@"media.png"];
                        //NSLog(@"%@",[accounts lastObject]);
                        [request setAccount:[accounts lastObject]];

                        //make request
                        [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                            NSLog(@"uploaded for %@",fanPages[key]);

                            NSString *respData = [[NSString alloc] initWithData:responseData
                                                                       encoding:NSUTF8StringEncoding];

                            NSLog(@"responseData %@",respData);
                            NSLog(@"urlResponse for %@",urlResponse);


                            [self performSelectorOnMainThread:@selector(removeProgressView) withObject:nil waitUntilDone:NO];

                        }];

                    }



                }
                else
                {
                    //upload to personal timeline
                    NSURL * url = [NSURL URLWithString:@"https://graph.facebook.com/me/photos"];
                    SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeFacebook requestMethod:SLRequestMethodPOST URL:url parameters:nil];
                    [request addMultipartData:[hashtags dataUsingEncoding:NSUTF8StringEncoding] withName:@"message" type:@"multipart/form-data" filename:nil];
                    [request addMultipartData:UIImageJPEGRepresentation(image, 0.9) withName:@"source" type:@"application/octet-stream" filename:@"media.png"];
                    [request setAccount:[accounts lastObject]];
                    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                        [self performSelectorOnMainThread:@selector(removeProgressView) withObject:nil waitUntilDone:NO];
                    }];
                }

            } else {

                //theres an error or they are not granted

            }

Any Ideas on what i'm doing wrong? I've scoured the internet for days on this one and everything seems correct.

解决方案

So it turns out that i was doing everything correctly, the problem was that if i left in:

[request setAccount:[accounts lastObject]];

This causes it to post on my behalf and wouldn't show up in the timeline. removing this code remedied the issue and all my posts are now showing up on the fan pages timeline from the fan page itself and not from my facebook user account.

Thanks Tobi, i kinda knew it was the access token and i thought i was passing it correctly, apparently i wasn't.

So just to re-iterate, don't use setAccount when posting to a fan page as it will overwrite the access_token with your user token and not the fan page token.

这篇关于Facebook Graph Api Fan页面照片上传(IOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆