【问题标题】:Facebook connect from iPhone app从 iPhone 应用程序连接 Facebook
【发布时间】:2011-04-24 11:14:59
【问题描述】:

我正在尝试将 Facebook 连接集成到我的 iPhone 应用,但出现错误(代码 10000):

did fail: The operation couldn’t be completed. (facebookErrDomain error 10000.)

当我尝试更新我的墙时。代码看起来很简单(虽然我不得不努力找到一些文档)。

- (void)viewDidLoad {
    [super viewDidLoad];

// Permissions
NSArray *permissions =  [[NSArray arrayWithObjects:@"publish_stream",@"read_stream",@"offline_access",nil] retain];     

// Connection 
Facebook *facebook = [[Facebook alloc] init];
[facebook authorize:@"MY_APP_ID" permissions:permissions delegate:self];

// Update my wall
NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MY_API_KEY", @"api_key", @"test", @"message", nil];
[facebook requestWithGraphPath:@"me/home" andParams:params andHttpMethod:@"POST" andDelegate:self];
}

// FBRequestDelegate

- (void)request:(FBRequest*)request didLoad:(id)result { 
NSArray* users = result; 
NSDictionary* user = [users objectAtIndex:0]; 
NSString* name = [user objectForKey:@"name"]; 
NSLog(@"Query returned %@", name); 
}

- (void)request:(FBRequest*)request didFailWithError:(NSError*)error {
    NSLog(@"did fail: %@", [error localizedDescription]);
}

- (void)request:(FBRequest*)request didReceiveResponse:(NSURLResponse*)response {
    NSLog(@"did r response");
}

无法真正找出问题所在。

非常感谢,

卢克

【问题讨论】:

  • 只是我还是应该 MY_APP_ID 是您的 Facebook API 应用程序名称的名称?关键源于我认为。
  • 我使用应用程序的 ID(15 位数字),这对于授权工作正常

标签: ios facebook facebook-ios-sdk


【解决方案1】:

确保您已声明应用的 API 密钥。

【讨论】:

    【解决方案2】:

    我想我想通了,我曾经有同样的问题。如果您首先使用正确的权限进行身份验证并确保您确实接受了所需的权限(publish_stream),您的问题应该得到解决。确保您确实可以按下“允许”按钮。在您当前的代码中,您不会看到它,因为您在身份验证后立即尝试发布消息,但身份验证尚未完成。因此,您很可能没有有效的 accessToken,从而阻止您将消息发布到墙上。

    // you perform authorization, but you don't wait for the user to accept the publish permission
    [facebook authorize:@"MY_APP_ID" permissions:permissions delegate:self]; 
    
    // you attempt to publish, but you don't have the permission to publish yet ...
    NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"MY_API_KEY", @"api_key", @"test", @"message", nil];
    [facebook requestWithGraphPath:@"me/home" andParams:params andHttpMethod:@"POST" andDelegate:self];
    

    如何解决此问题?

    这是我修复它的方式(请注意 DLog() 是 NSLog() 的宏):

    /* The operationQueue array is used to keep track of operations that need to be 
       completed in order (e.g. if we aren't logged in when we want to post, first 
       log in, then post - this way we'll make sure we always have a valid 
       accessToken. */
    - (id)initWithDelegate:(id <ServiceDelegate>)serviceDelegate
    {
       self = [super init];
       if (self) 
       {
          [self setDelegate:serviceDelegate];   
          userId = nil;
          operationQueue = [[NSMutableArray alloc] init];
    
          AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
          facebook = appDelegate.facebook;
       }
       return self;
    }
    
    - (id)init 
    {
       return [self initWithDelegate:nil];
    }
    
    - (void)dealloc 
    {
       [operationQueue release];
       [super dealloc];
    }
    
    #pragma mark - Instance methods
    
    - (void)login 
    {   
       if (![facebook isSessionValid]) {
          SEL authorizationSelector = @selector(performAuthorization);
          NSValue *selector = [NSValue valueWithPointer:authorizationSelector];
          NSDictionary *dictionary = [NSDictionary dictionaryWithObject:selector forKey:@"selector"];
          [operationQueue addObject:dictionary];
    
          [self runOperations]; 
       }      
    }
    
    - (void)logout 
    {
       SEL logoutSelector = @selector(performLogout);
       NSValue *selector = [NSValue valueWithPointer:logoutSelector];
       NSDictionary *dictionary = [NSDictionary dictionaryWithObject:selector forKey:@"selector"];
       [operationQueue addObject:dictionary];
    
       [self runOperations]; 
    }
    
    - (void)postMessage:(NSString *)message 
    {   
       NSArray *objects = [NSArray arrayWithObjects:message, nil];
       NSArray *keys = [NSArray arrayWithObjects:@"message", nil];
       NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithObjects:objects forKeys:keys];
       NSDictionary *dictionary = [NSDictionary dictionaryWithObject:parameters forKey:@"parameters"];
    
       SEL postMessageSelector = @selector(performPostMessageWithDictionary:);
       NSValue *selector = [NSValue valueWithPointer:postMessageSelector];
       NSDictionary *operationDictionary = [NSDictionary dictionaryWithObjectsAndKeys:selector, @"selector", dictionary, @"parameters", nil];
       [operationQueue addObject:operationDictionary];
    
       if (![facebook isSessionValid]) {
          [self performSelectorOnMainThread:@selector(performAuthorization) withObject:nil waitUntilDone:NO];
       } else {
          [self runOperations];
       }
    }
    
    #pragma mark - Private methods 
    
    - (void)runOperations 
    {
        DLog(@"running operations ...");
    
        for (NSDictionary *operationDictionary in operationQueue) {
            NSValue *value = [operationDictionary objectForKey:@"selector"];
            SEL selector = [value pointerValue];
            NSDictionary *parameters = [operationDictionary objectForKey:@"parameters"];
            [self performSelectorOnMainThread:selector withObject:parameters waitUntilDone:YES];
        }
    
        [operationQueue removeAllObjects];
    }
    
    - (void)performLogout {
       [facebook logout:self];
    }
    
    - (void)performAuthorization {   
       NSArray *permissions = [NSArray arrayWithObject:@"publish_stream"];
       [facebook authorize:permissions delegate:self];   
    }
    
    - (void)performPostMessageWithDictionary:(NSDictionary *)dictionary {
       NSMutableDictionary *parameters = [dictionary objectForKey:@"parameters"];
       NSString *encodedToken = [facebook.accessToken stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
       NSString *graphPath = [NSString stringWithFormat:@"me/feed?access_token=%@", encodedToken];
       FBRequest *request = [facebook requestWithGraphPath:graphPath
                                                 andParams:parameters 
                                             andHttpMethod:@"POST" 
                                               andDelegate:self];
       if (!request) {
          DLog(@"error occured when trying to create FBRequest object with graph path : %@", graphPath);
       }
    }
    
    /* make sure your interface conforms to the FBRequestDelegate protocol for 
           extra debug information, but this is not required */
    #pragma mark - Facebook request delegate
    
    - (void)requestLoading:(FBRequest *)request 
    {
       DLog(@"requestLoading:");
    }
    
    - (void)request:(FBRequest *)request didReceiveResponse:(NSURLResponse *)response 
    {
       DLog(@"request:didReceiveResponse:");
    }
    
    - (void)request:(FBRequest *)request didFailWithError:(NSError *)error 
    {
       DLog(@"error occured when trying to perform request to Facebook : %@", error);
    }
    
    - (void)request:(FBRequest *)request didLoad:(id)result 
    {
       DLog(@"request:didLoad: %@", result);
    }
    
    - (void)request:(FBRequest *)request didLoadRawResponse:(NSData *)data 
    {
       DLog(@"request:didLoadRawResponse");
    }
    
    /* make sure your interface conforms to the FBSessionDelegate protocol! */
    #pragma mark - Facebook session delegate
    
    /* if there are still operations in the queue that need to be completed, 
    continue executing the operations, otherwise inform out delegate that login 
    is completed ... */
    - (void)fbDidLogin 
    {   
       if ([operationQueue count] > 0) {
          [self runOperations]; 
       }
    
       if ([self.delegate respondsToSelector:@selector(serviceDidLogin:)]) 
       {
          [self.delegate serviceDidLogin:self];
       }
    }
    
    - (void)fbDidNotLogin:(BOOL)cancelled 
    {
       if ([self.delegate respondsToSelector:@selector(serviceLoginFailed:)]) 
       {
          [self.delegate serviceLoginFailed:self];
       }
    }
    
    - (void)fbDidLogout 
    {   
       if ([self.delegate respondsToSelector:@selector(serviceDidLogout:)]) 
       {
          [self.delegate serviceDidLogout:self];
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多