gpt4 book ai didi

ios - Facebook Connect 访问用户的 FB session /访问 token ID?

转载 作者:行者123 更新时间:2023-11-29 03:51:57 25 4
gpt4 key购买 nike

我已将我的应用程序配置为正确允许用户通过 FB Connect 3.1 SDK(适用于 iOS 6)登录,并请求电子邮件权限:

NSArray *permissons = [[NSArray alloc] initWithObjects:@"email", nil];
[FBSession openActiveSessionWithReadPermissions:permissons
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];

之后,我需要确定用户的 FB session ID。这与访问 token 有什么不同吗?我相信 session / token ID 可能以“AAAFTZCN54f1EBA”开头?我如何在身份验证后检索此信息?

提前致谢!

最佳答案

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
internetActive=YES;

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch.

self.viewController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

self.navController=[[UINavigationController alloc] initWithRootViewController:self.viewController];

[self.navController.navigationBar setBarStyle:UIBarStyleBlackTranslucent];
self.window.rootViewController = self.navController;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
_internetReachable = [Reachability reachabilityForInternetConnection];
[_internetReachable startNotifier];

if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
// Yes, so just open the session (this won't display any UX).
[self openSession];
} else {
// No, display the login page.
[self showLoginView];
}

[self.window makeKeyAndVisible];
return YES;
}


-(void)checkNetworkStatus:(NSNotification *)notice
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Internet Connection" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
// called after network status changes
NetworkStatus internetStatus = [_internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
alert.message=@"The internet is down.";
internetActive = NO;
break;
}
case ReachableViaWiFi:
{
alert.message=@"The internet is working via WIFI.";
internetActive = YES;
break;
}
case ReachableViaWWAN:
{
alert.message=@"The internet is working via WWAN.";
internetActive = YES;
break;
}
}
[alert show];
}

#pragma mark- Facebook methods

-(void)getEmailId
{
[facebook requestWithGraphPath:@"me" andDelegate:self];
}

- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [FBSession.activeSession handleOpenURL:url];
}


-(void)fblogout{
NSLog(@"logout called ");
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"fbemail"];
[FBSession.activeSession closeAndClearTokenInformation];
[facebook logout];
}
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen: {
UIViewController *topViewController =[self.navController topViewController];
if ([topViewController isKindOfClass:[Sport_InceptionViewController class]]) {
[topViewController dismissViewControllerAnimated:YES completion:nil];
// [topViewController dismissModalViewControllerAnimated:YES];
}
}

// Initiate a Facebook instance
facebook = [[Facebook alloc]
initWithAppId:FBSession.activeSession.appID
andDelegate:nil];

// Store the Facebook session information
facebook.accessToken = FBSession.activeSession.accessToken; **Here is you token**
facebook.expirationDate = FBSession.activeSession.expirationDate;

if (![[NSUserDefaults standardUserDefaults] valueForKey:@"fbemail"]) {
[MBProgressHUD showHUDAddedTo:self.viewController.view animated:YES];
[self getEmailId];
}

break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Once the user has logged in, we want them to
// be looking at the root view.
[self.navController popToRootViewControllerAnimated:NO];

[FBSession.activeSession closeAndClearTokenInformation];
facebook = nil;

[self showLoginView];
break;
default:
break;
}

[[NSNotificationCenter defaultCenter]
postNotificationName:FBSessionStateChangedNotification
object:session];

if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}

- (void)openSession
{
if (internetActive) {
NSArray *permissions=[NSArray arrayWithObjects:@"read_stream",@"email",nil];

[FBSession openActiveSessionWithPermissions:permissions allowLoginUI:YES completionHandler: ^(FBSession *session,FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}else
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:@"Internet Not Connected" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}

#pragma mark- request delegate methods

- (void)request:(FBRequest *)request didLoad:(id)result
{
NSLog(@"request did load successfully....");

// __block NSDictionary *dictionary=[[NSDictionary alloc] init];

if ([result isKindOfClass:[NSDictionary class]]) {
NSDictionary* json = result;

NSLog(@"email id is %@",[json valueForKey:@"email"]);
NSLog(@"json is %@",json);

[[NSUserDefaults standardUserDefaults] setValue:[json valueForKey:@"email"] forKey:@"fbemail"];
[self.viewController login:YES];
}
}

- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@"" message:@"Server not responding.." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];

[alertView show];

[self fblogout];
[self showLoginView];
NSLog(@"request did fail with error");
}


- (void)showLoginView
{
UIViewController *topViewController = [self.navController topViewController];

if (![topViewController isKindOfClass:[MyViewController class]]) {

MyViewController *loginViewController=[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
[topViewController presentViewController:loginViewController animated:YES completion:nil];
}
else {
MyViewController* loginViewController=(MyViewController*)topViewController;
[loginViewController loginFailed];
}
}

关于ios - Facebook Connect 访问用户的 FB session /访问 token ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16980038/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com