问题
I have developed an iphone app that needs the email address of a facebook friend. Assuming that your friend has the app and has granted permissions for email and offline_access, then I believe it is possible to use:
[facebook requestWithGraphPath:@"FRIEND_ID?fields=id,email" andDelegate:self];
to obtain their email address. For testing purposes and for submission to apple, how do you create within an ios app a facebook test user using the facebook sdk? Facebook provides the ability to create test users (http://developers.facebook.com/docs/test_users/), but I cannot understand how to use the information on this website within my ios app using their sdk/graph api.
回答1:
Ok, I have been looking into this as well, and here is what I've come up with.
According to the documentation, one should be able to create a test user by calling upon the graph API as follows:
NSString *urlString = [NSString stringWithFormat:@"%@/accounts/test-users", kAppId];
[facebook requestWithGraphPath:urlString andParams:params andHttpMethod:@"POST" andDelegate:self];
where params
is an NSMutableDictionary containing some or all of the parameters as detailed at https://developers.facebook.com/docs/test_users/.
However, I have found that the API desires an app access token to create a test user, and the method requestWithGraphPath:andParams:andHttpMethod:andDelegate:
within the SDK file Facebook.m
actually sends a user access token. Even if you specifically set the app access token within the params
dictionary, it will be overwritten with the user access token within this method.
It would appear that there are two ways around this predicament. We could monkey with the Facebook SDK and take this into account, which is definitely not recommended. Or we could resort to standard HTTP requests, and take matters into our own hands. Here is how I handled the second approach.
First we need the app access token, which can be retrieved by making a request to:
https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials
.
We then use the app access token returned from this request in our next request to:
https://graph.facebook.com/APP_ID/accounts/test-users?installed=true&name=FULL_NAME&permissions=read_stream&method=post&access_token=APP_ACCESS_TOKEN
The overall format for both requests is the same. Here is what the entire request will look like, in code, for the second of the two requests.
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];
Hopefully this will help get you over the hump into experimenting with test users.
来源:https://stackoverflow.com/questions/7700338/how-do-you-create-a-facebook-test-user-in-ios-using-the-facebook-sdk