首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用于iphone的Facebook Graph API传递访问令牌

用于iphone的Facebook Graph API传递访问令牌
EN

Stack Overflow用户
提问于 2011-05-09 18:53:24
回答 2查看 2.2K关注 0票数 2

我正在使用Facebook Graph API,例如API

它正在验证view中是否出现了登录凭据,并在一个视图中调用所有方法。我想在其他视图中使用这些方法,但如何将访问令牌传递给其他视图(维护登录会话)。以下是登录的代码

代码语言:javascript
运行
复制
    - (void)viewDidAppear:(BOOL)animated {

    //Facebook Application ID

    NSString *client_id = @"142759389130183";

    //alloc and initalize our FbGraph instance
    self.fbGraph = [[FbGraph alloc] initWithFbClientID:client_id];

    //begin the authentication process.....
    //[fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) 
    //                   andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];



        [fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access" andSuperView:self.view];


}
- (void)fbGraphCallback:(id)sender {

    if ( (fbGraph.accessToken == nil) || ([fbGraph.accessToken length] == 0) ) {

        NSLog(@"You pressed the 'cancel' or 'Dont Allow' button, you are NOT logged into Facebook...I require you to be logged in & approve access before you can do anything useful....");

        //restart the authentication process.....
    //  [fbGraph authenticateUserWithCallbackObject:self andSelector:@selector(fbGraphCallback:) 
            //               andExtendedPermissions:@"user_photos,user_videos,publish_stream,offline_access,user_checkins,friends_checkins"];

    //  StartPage *startPage = [[StartPage alloc]initWithNibName:@"StartPage" bundle:nil];
        //[self.navigationController pushViewController:startPage animated:YES];



        //[startPage release];

        [self dismissModalViewControllerAnimated:YES];
    } else {
        //pop a message letting them know most of the info will be dumped in the log
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Welcome" message:@"You are logged into facebook" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
        [alert show];
        [alert release];

        NSLog(@"------------>CONGRATULATIONS<------------, You're logged into Facebook...  Your oAuth token is:  %@", fbGraph.accessToken);



    }

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-05-09 20:42:18

代码语言:javascript
运行
复制
 -(IBAction)postMeFeedButtonPressed:(id)sender {

postFeedCustom *detailViewController = [[postFeedCustom alloc] initWithNibName:@"postFeedCustom" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];

}

代码语言:javascript
运行
复制
-(void)postMeFeedCustomized{
NSMutableDictionary *variables = [NSMutableDictionary dictionaryWithCapacity:4];

[variables setObject:postString forKey:@"message"];
//[variables setObject:@"http://bit.ly/bFTnqd" forKey:@"link"];
//[variables setObject:@"This is the bolded copy next to the image" forKey:@"name"];
//[variables setObject:@"This is the plain text copy next to the image.  All work and no play makes Jack a dull boy." forKey:@"description"];

FbGraphResponse *fb_graph_response = [fbGraph doGraphPost:@"me/feed" withPostVars:variables];
NSLog(@"postMeFeedButtonPressed:  %@", fb_graph_response.htmlResponse);

//parse our json
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil];   
[parser release];

//let's save the 'id' Facebook gives us so we can delete it if the user presses the 'delete /me/feed button'

self.feedPostId = (NSString *)[facebook_response objectForKey:@"id"];
NSLog(@"feedPostId, %@", feedPostId);
NSLog(@"Now log into Facebook and look at your profile...");

}

在您希望用户发布其自定义消息的另一个类中...你可以这样做..

代码语言:javascript
运行
复制
  -(void) viewDidLoad{
[super viewDidLoad];
appDelegate=(oAuth2TestAppDelegate*)[[UIApplication sharedApplication]delegate];

}

代码语言:javascript
运行
复制
 -(IBAction)FeedPost:(id)sender{
postString=postField.text;
[appDelegate.viewController postMeFeedCustomized];
postField.text=@"";
[postField resignFirstResponder];

}

你看到了吗??在按钮的这个方法中,我调用了postfeedcustomized方法,并将文本字段参数存储在postString中,这就是我在postfeedcustomized方法中发布的postStirng。请参阅此说明[variables setObject:postString forKey:@"message"];

这里的post字符串是字符串...which在viewController 2中,...it携带由user..now输入的消息这是用于发布消息的代码从另一个view...when按钮被按下用于发布馈送它将转到另一个视图控制器,其中是textbox..there用户在输入消息后输入他的message...and方法postfeedcustomized正从该视图控制器被调用。..我已经根据您的需要和从另一个视图的post图片对状态控制器执行了此操作。

票数 0
EN

Stack Overflow用户

发布于 2011-05-09 19:18:40

我也使用Facebook API。在我的代码中,我有一个"FacebookLogger“,他是一个带有Facebook对象的单例对象。

为了存储会话,我使用NSUserDefaults (find docs here)

代码语言:javascript
运行
复制
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.accessToken forKey:@"AccessToken"];
[[NSUserDefaults standardUserDefaults] setObject:m_Facebook.expirationDate forKey:@"ExpirationDate"];
[[NSUserDefaults standardUserDefaults] synchronize];

其中m_Facebook是我单例中的Facebook对象。在此之后,我可以使用以下命令捕获访问:

代码语言:javascript
运行
复制
m_Facebook.accessToken    = [[NSUserDefaults standardUserDefaults] stringForKey:@"AccessToken"];

m_Facebook.expirationDate = (NSDate *) [[NSUserDefaults standardUserDefaults] objectForKey:@"ExpirationDate"];
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5935846

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档