我正在编写一种方法,该方法从我的 Parse 数据库中获取 facebook 用户记录,然后在 StackMob 上重新创建记录。当我调用 createUserWithFacebookToken 它返回此错误:
[48887:c07] 错误域 = HTTP 代码 = 401“操作无法完成。(HTTP 错误 401。)”用户信息 = 0xa6a9e60 {错误 = 登录失败:}
- (IBAction)fetchAllUsers:(id)sender {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.parse.com/1/users/"]];
[request setValue:@"XXXX" forHTTPHeaderField:@"X-Parse-Application-Id"];
[request setValue:@"XXXX" forHTTPHeaderField:@"X-Parse-Master-Key"];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"didReceiveResponse");
[self.responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"didFailWithError");
NSLog(@"Connection failed: %@", [error description]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSLog(@"connectionDidFinishLoading");
NSLog(@"Succeeded! Received %d bytes of data",[self.responseData length]);
// convert to JSON
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
NSArray *allUsers = [res objectForKey:@"results"];
// show all values
for (int i=0;i<allUsers.count;i++) {
id value = [allUsers objectAtIndex:i];
NSString *username = [value objectForKey:@"username"];
NSDictionary *authData = [value objectForKey:@"authData"];
NSDictionary *facebook = [authData objectForKey:@"facebook"];
NSString *accessToken = [facebook objectForKey:@"access_token"];
if (accessToken) {
NSDictionary *aUser = [NSDictionary dictionaryWithObjectsAndKeys:accessToken, @"accessToken", username, @"username", nil];
// [_userInfo addObject:aUser];
NSLog(@"%@", aUser);
[self processTheUser:aUser];
}
}
}
- (void)processTheUser:(NSDictionary *)user {
[self.client createUserWithFacebookToken:[user valueForKey:@"accessToken"] username:[user valueForKey:@"username"] onSuccess:^(NSDictionary *result) {
NSLog(@"%@", result);
} onFailure:^(NSError *error) {
NSLog(@"%@", error);
}];
}