好的,我也一直在研究这个,这就是我想出的。
根据文档,应该能够通过调用图形 API 来创建测试用户,如下所示:
NSString *urlString = [NSString stringWithFormat:@"%@/accounts/test-users", kAppId];
[facebook requestWithGraphPath:urlString andParams:params andHttpMethod:@"POST" andDelegate:self];
其中params 是一个 NSMutableDictionary,其中包含https://developers.facebook.com/docs/test_users/ 中详述的部分或全部参数。
但是,我发现 API 需要应用访问令牌来创建测试用户,而 SDK 文件 Facebook.m 中的方法 requestWithGraphPath:andParams:andHttpMethod:andDelegate: 实际上会发送用户访问令牌。即使您在params 字典中专门设置了应用访问令牌,它也会被此方法中的用户访问令牌覆盖。
似乎有两种方法可以解决这个困境。我们可以使用 Facebook SDK 并考虑到这一点,这绝对不推荐。或者我们可以求助于标准的 HTTP 请求,并自己处理事情。这是我处理第二种方法的方法。
首先,我们需要应用访问令牌,可以通过向以下地址发出请求来检索该令牌:
https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials.
然后,我们在下一个请求中使用从该请求返回的应用访问令牌:
https://graph.facebook.com/APP_ID/accounts/test-users?installed=true&name=FULL_NAME&permissions=read_stream&method=post&access_token=APP_ACCESS_TOKEN
两个请求的整体格式相同。这是两个请求中的第二个请求的整个请求在代码中的样子。
NSString *urlString = @"https://graph.facebook.com/APP_ID/accounts/test-users";
NSURL *testUserUrl = [NSURL URLWithString:urlString];
NSMutableURLRequest *testUserRequest = [[NSMutableURLRequest alloc] initWithURL:testUserUrl];
[testUserRequest setHTTPMethod:@"POST"];
[testUserRequest addValue:@"text/plain" forHTTPHeaderField:@"content-type"];
NSString *bodyString = @"installed=true&permissions=read_stream&access_token=APP_ACCESS_TOKEN";
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
[testUserRequest setHTTPBody:bodyData];
[[NSURLConnection alloc] initWithRequest:testUserRequest delegate:self];
[testUserRequest release];
希望这将帮助您克服困难,开始对测试用户进行试验。