简体   繁体   English

如何使用Facebook SDK在iOS中创建Facebook测试用户

[英]How do you create a facebook test user in ios using the facebook sdk

I have developed an iphone app that needs the email address of a facebook friend. 我已经开发了一个iPhone应用程序,需要一个Facebook朋友的电子邮件地址。 Assuming that your friend has the app and has granted permissions for email and offline_access, then I believe it is possible to use: 假设您的朋友拥有该应用程序,并已授予对电子邮件和offline_access的权限,那么我相信可以使用:

[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? 为了进行测试并提交给Apple,您如何在ios应用程序中使用facebook sdk创建facebook测试用户? 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. Facebook提供了创建测试用户(http://developers.facebook.com/docs/test_users/)的功能,但是我无法理解如何使用sdk / graph api在我的ios应用程序中使用此网站上的信息。

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: 根据文档,应该可以通过调用图形API来创建测试用户,如下所示:

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/ . 其中params是NSMutableDictionary,其中包含部分或全部参数,如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. 然而,我发现,API期望的应用程序访问令牌创建测试用户,且该方法requestWithGraphPath:andParams:andHttpMethod:andDelegate:在SDK文件中Facebook.m实际发送用户访问令牌。 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. 即使您在params字典中专门设置了应用程序访问令牌,该方法中的用户访问令牌也会覆盖它。

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. 我们可以使用Facebook SDK并考虑到这一点,绝对不建议这样做。 Or we could resort to standard HTTP requests, and take matters into our own hands. 或者,我们可以求助于标准的HTTP请求,并由我们自己处理。 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 . 首先,我们需要应用访问令牌,可以通过向以下请求发出请求来获取该令牌: https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&grant_type=client_credentials : 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 然后,我们在下一个请求中使用从此请求返回的应用访问令牌: https://graph.facebook.com/APP_ID/accounts/test-users?installed=true&name=FULL_NAME&permissions=read_stream&method=post&access_token=APP_ACCESS_TOKEN : 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. 希望这将帮助您轻松进行测试用户的试验。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM