Facebook的空访问令牌

时间:2012-11-05 15:11:01

标签: iphone objective-c ios facebook

过去两天你一直在努力解决这个问题。我从来没有遇到过这个问题,但无论我做什么,从facebook返回的访问令牌总是为空。

使用的代码来自facebook connect IOS sdk。我在其他应用程序中使用完全相同的代码,但没有任何问题,但由于某些原因,这现在不起作用。

也没有打印错误代码。

感谢。

2 个答案:

答案 0 :(得分:0)

我在sessionChangeState中使用,此代码:

FBRequest *request = [FBRequest requestForMe];
[request startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary *result, NSError *error) {
    NSLog(@"access Token %@", FBSession.activeSession.accessToken);
}];

然后它返回我的访问令牌,试试吧!

答案 1 :(得分:0)

尝试此代码。您将通过self.facebook.accessToken获取访问令牌。 在视图中调用facebookbtnclicked方法确实加载或在任何地方获取访问令牌。

facebook = [[Facebook alloc] initWithAppId:@"227757844008650"];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"])
    {

        facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];

    }
-(void)facebookbtnclicked
{
    NSLog(@"facebook btn clicked");
    [self loginToFacebook:self];
}

- (BOOL) isFaceBookLoggedIn
{
    if (self.facebook.accessToken)
    {
        return YES;

    }
    else {
        return NO;
    }
}

- (void) logoutFromFacebook: (id) logoutDelegate
{
    fbServiceRequestingobj = logoutDelegate;
    [facebook logout:self];
}

- (void)loginToFacebook:(id) loginDelegate
{
    NSLog(@"login facebook method");

    fbServiceRequestingobj = loginDelegate;

    NSArray* permissions = [[NSArray alloc] initWithObjects:
                            @"publish_stream", nil];
    [facebook authorize:permissions delegate:self];

}
-(void) getuserdata
{

    NSLog(@"[appDelegate getuserdata]");

    if (fbServiceRequestingobj) 
    {
        fbServiceRequestingobj = nil;
    }

    [facebook requestWithGraphPath:@"me" andDelegate:self];


    [facebook requestWithGraphPath:@"me/friends" andDelegate:self];

}
- (void)fbDidLogout
{
    NSLog(@"Did Logout");

    if (fbServiceRequestingobj)
    {
        if ([fbServiceRequestingobj respondsToSelector:@selector(fbDidLogout)])
        {
            [fbServiceRequestingobj performSelector:@selector(fbDidLogout)];
        }
    }
    fbServiceRequestingobj = nil;
}


- (void)request:(FBRequest *)request didFailWithError:(NSError *)error
{

    if (fbServiceRequestingobj)
    {
        if ([fbServiceRequestingobj respondsToSelector:@selector(request:didFailWithError:)])
        {
            [fbServiceRequestingobj request:request didFailWithError:error];
        }
    }
    fbServiceRequestingobj = nil;
}

-(void)fbDidNotLogin:(BOOL)cancelled
{

    if (fbServiceRequestingobj)
    {
        if ([fbServiceRequestingobj respondsToSelector:@selector(fbDidNotLogin:)])
        {
            //[fbServiceRequestingobj fbDidNotLogin:cancelled];
        }
    }
    fbServiceRequestingobj = nil;
}


- (void)fbDidLogin
{

    NSLog(@"fbDidLogin");
    //[facebook requestWithGraphPath:@"me/friends" andDelegate:self];
    NSLog(@"accesstoken########%@",self.facebook.accessToken);
}


- (void)requestFinished:(ASIHTTPRequest *)request
{
    count++;
    NSError *error = [request error];

    if (!error)
    {
        NSString *response = [request responseString];

        NSDictionary *respdic=[response JSONValue];
        if(![[respdic valueForKey:@"id"] isEqualToString:@""])
        {


            UIAlertView *alert1=[[UIAlertView alloc] initWithTitle:@"" message:@"Photo posted successfully." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert1 show];
        }
    }
}

- (void)request:(FBRequest *)request didLoad:(id)result
{


    if ([result isKindOfClass:[NSArray class]])
    {
        result = [result objectAtIndex:0];
    }

    if ([result isKindOfClass:[NSDictionary class]])
    {

        }   


    }
    if ([result isKindOfClass:[NSData class]])
    {

        UIImage *profilePicture = [[UIImage alloc] initWithData: result];

    }


    if (fbServiceRequestingobj)
    {
        if ([fbServiceRequestingobj respondsToSelector:@selector(request:didLoad:)])
        {
            [fbServiceRequestingobj request:request didLoad:result];
        }
    }

    fbServiceRequestingobj = nil;

};