Facebook OAuth api登录问题 [英] Facebook OAuth api login problems

查看:124
本文介绍了Facebook OAuth api登录问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的FBSessionDelegate方法没有被调用,而accessToken并且没有设置过期日期值,所以我不认为我曾经登录过。



我恢复了一个非常简单的应用程序,只有两个按钮(登录,注销)和标签以查看状态信息。



以下是视图控制器的代码,其中进行所有处理:

 code> // 
#import< UIKit / UIKit.h>
#importFacebook.h

@interface facebook_login_testViewController:UIViewController< FBSessionDelegate> {

UIButton * loginButton;
UIButton * logoutButton;
UILabel * info;

Facebook * facebook;

}

@property(非原子,保留)Facebook * Facebook;

- (void)loggingIn;
- (void)loggingOut;

@end

  #importfacebook_login_testViewController.h

@implementation facebook_login_testViewController
@synthesize Facebook;

- (void)dealloc
{
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

#pragma mark - 查看生命周期

- (void)viewDidLoad
{
[super viewDidLoad];

loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[loginButton setTitle:@登录forState:UIControlStateNormal];
[loginButton addTarget:self action:@selector(loggingIn)forControlEvents:UIControlEventTouchUpInside];
loginButton.frame = CGRectMake(200,200,200,50);

logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[logoutButton setTitle:@Log OutforState:UIControlStateNormal];
[logoutButton addTarget:self action:@selector(loggingOut)forControlEvents:UIControlEventTouchUpInside];
logoutButton.frame = CGRectMake(200,300,200,50);

info = [[UILabel alloc] initWithFrame:CGRectMake(200,400,400,600)];
info.numberOfLines = 0;

[self.view addSubview:loginButton];
[self.view addSubview:logoutButton];
[self.view addSubview:info];

info.text = @等待登录... \\\
\\\
按下登录按钮。

facebook = [[Facebook alloc] initWithAppId:@159 ........... 5andDelegate:self];

NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
if([defaults objectForKey:@FBAccessTokenKey]
&&&&default; ]。
facebook.expirationDate = [defaults objectForKey:@FBExpirationDateKey];
}

}


- (void)viewDidUnload
{
[super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation)interfaceOrientation
{
//为支持的方向返回YES
return YES;
}

- (void)loggingIn {
if(![facebook isSessionValid]){
[facebook authorize:nil];
}
}

- (void)loggingOut {
info.text = [NSString stringWithFormat:@\\\
access token:%@ \\\
expiration date: %@ \\\
facebood描述:%@ \\\
,facebook.accessToken,facebook.expirationDate,Facebook];
[facebook logout:self];

}

- (BOOL)应用程序:(UIApplication *)应用程序handleOpenURL:(NSURL *)url {

return [facebook handleOpenURL:url ]。
}

- (void)fbDidLogin {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[facebook accessToken] forKey:@FBAccessTokenKey];
[defaults setObject:[facebook expirationDate] forKey:@FBExpirationDateKey];
[默认同步];

}

@end

plist文件设置如下





这真的看起来太简单了。当我点击登录按钮时,我可以通过 authorize:permission ,然后 authorize:permissions localAppId:localAppId 就我可以追溯到,我看不出有什么看起来会导致 isSessionValid 变为TRUE。



我等待然后按退出按钮,并查看一些Facebook参数,以防异常发生,并且仍然为空。



有人可以看到我缺少什么吗?

解决方案

网上有几个例子说明了如何工作,但是Facebook SDK随着一个插入appId的演示应用程序(DemoApp)一起发布。 (如果您没有插入appId值,它会在检查该值时被杀死。)



授权过程中发生的是 [facebook authorize:permissions] 被调用。 (权限不应为零)



这导致呼叫 [self authorize:permissions localAppId:nil] ,然后调用 [self authorizeWithFBAppAuth:YES safariAuth:YES] 。在这种方法中,可以尝试连接到Facebook进行授权的三种方式。



在这些异步方法的每一个中,应用程序委托是处理结果回调的委托,作为应用程序的一部分:(UIApplication *)应用程序handleOpenURL: (NSURL *)url 。如果在其他在线教程中提到这一点,很容易错过,因为它们通常在应用程序委托中进行所有处理,从我所看到的。在我的情况下,我在主视图控制器中进行了主要处理。



在我的例子中,修复是移动应用程序:(UIApplication *)应用程序handleOpenURL:(NSURL *)url 方法给应用程序委托并将其指向Facebook对象,如下所示:

   - (BOOL)应用程序:(UIApplication *)application handleOpenURL :( NSURL *)url {
return [[controller facebook] handleOpenURL:url];
}

之后,其他的facebook委托方法(fbDidLogin,fbDidLogout)被调用,我收到了accessToken和expirationDate。所以看起来这样现在正在工作。


I am stumped by the Facebook API for logging in. I can't find anything helpful on the net.

My FBSessionDelegate methods are not being called, and accessToken and expirationDate values are not being set, so I don't think I am ever logged in.

I reverted to a very simple app, with only two buttons (log in, log out) and a label to see status information.

Here is the code for the view controller, where all the processing takes place:

// 
#import <UIKit/UIKit.h>
#import "Facebook.h"

@interface facebook_login_testViewController : UIViewController <FBSessionDelegate>{

    UIButton *loginButton;
    UIButton *logoutButton;
    UILabel *info;

    Facebook *facebook;

}

@property (nonatomic, retain) Facebook *facebook;

- (void) loggingIn;
- (void) loggingOut;

@end

and

#import "facebook_login_testViewController.h"

@implementation facebook_login_testViewController
@synthesize facebook;

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    loginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [loginButton setTitle: @"Log In" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loggingIn) forControlEvents:UIControlEventTouchUpInside];
    loginButton.frame = CGRectMake(200, 200, 200, 50);

    logoutButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [logoutButton setTitle: @"Log Out" forState:UIControlStateNormal];
    [logoutButton addTarget:self action:@selector(loggingOut) forControlEvents:UIControlEventTouchUpInside];
    logoutButton.frame = CGRectMake(200, 300, 200, 50);

    info = [[UILabel alloc] initWithFrame:CGRectMake(200, 400, 400, 600)];
    info.numberOfLines = 0;

    [self.view addSubview:loginButton];
    [self.view addSubview:logoutButton];
    [self.view addSubview:info];

    info.text = @"Waiting to log in...\n\nPress the login button.";

    facebook = [[Facebook alloc] initWithAppId:@"159...........5" andDelegate:self];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }

}


- (void)viewDidUnload
{
    [super viewDidUnload];        
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}

- (void) loggingIn{
    if (![facebook isSessionValid]) {
        [facebook authorize:nil];
    }
}

- (void) loggingOut{
    info.text = [NSString stringWithFormat:@"\naccess token: %@\nexpiration date: %@\nfacebood description:%@\n",facebook.accessToken, facebook.expirationDate, facebook];
    [facebook logout:self];

}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {

    return [facebook handleOpenURL:url]; 
}

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[facebook accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[facebook expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

}

@end

The .plist file is set up like this

This really looks too simple to get wrong. When I click the login button, I can trace through authorize:permission, and then authorize:permissions localAppId:localAppId, and as far as I can trace it, I don't see anything that looks like it's going to results in isSessionValid becoming TRUE.

I wait and then press the log off button, and take a look at some Facebook parameters, in case something asynchronous needed to occur, and they are still null.

Can someone see what I am missing?

解决方案

There are few examples on the net that show how this works, but the Facebook SDK is distributed with a demo app (DemoApp) that works once you plug in the appId. (If you don't plug in the appId value, it will get killed when it checks for that value.)

What is happening in the authorization process is that [facebook authorize:permissions] is called. (permissions should not be nil)

This leads to a call to [self authorize:permissions localAppId:nil], which then calls [self authorizeWithFBAppAuth:YES safariAuth:YES]. In that method, three ways of connecting to facebook for authorization can be tried.

In each of these asynchronous methods, the application delegate is the delegate that handles the resulting callback as part of the to application:(UIApplication *)application handleOpenURL:(NSURL *)url. If this was mentioned in the other online tutorials, it's easy to miss, because they generally do all of the processing within the application delegate, from what I saw. In my case, I did the main processing within the main view controller.

The fix, in my case, was to move the application:(UIApplication *)application handleOpenURL:(NSURL *)url method to the application delegate and point it to the facebook object like this:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  return [[controller facebook] handleOpenURL:url];
}

After that, the other facebook delegate methods (fbDidLogin, fbDidLogout) were called and I received the accessToken and expirationDate. So it looks like this is working now.

这篇关于Facebook OAuth api登录问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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