如何在ios中使用facebook sdk创建一个Facebook测试用户 [英] How do you create a facebook test user in ios using the facebook sdk

查看:313
本文介绍了如何在ios中使用facebook sdk创建一个Facebook测试用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个需要Facebook朋友的电子邮件地址的iphone应用程序。假设您的朋友拥有该应用并已授予电子邮件和offline_access的权限,那么我相信可以使用:

  Facebook requestWithGraphPath:@FRIEND_ID?fields = id,emailandDelegate:self]; 

获取他们的电子邮件地址。为了测试目的和提交给苹果,您如何在ios应用程序中创建一个使用Facebook sdk的Facebook测试用户? Facebook提供了创建测试用户的能力(http://developers.facebook.com/docs/test_users/),但是我不明白如何在我的ios应用程序中使用其sdk / graph api使用本网站上的信息。

解决方案

好的,我一直在研究这个,这里是我想出来的。 >

根据文档,应该能够通过调用图形API来创建测试用户,如下所示:

  NSString * urlString = [NSString stringWithFormat:@%@ / accounts / test-users,kAppId]; 
[facebook requestWithGraphPath:urlString andParams:params andHttpMethod:@POSTandDelegate:self];

其中 params 是一个NSMutableDictionary,包含一些或所有参数,详见 https://developers.facebook.com/docs/test_users/



但是,我发现API需要一个应用程序访问令牌来创建测试用户,并且方法 requestWithGraphPath:andParams:andHttpMethod :andDelegate:在SDK文件 Facebook.m 实际上发送一个用户访问令牌。即使您在 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 / plainforHTTPHeaderField:@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];

希望这将有助于让您超越测试用户的实验。


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.

解决方案

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.

这篇关于如何在ios中使用facebook sdk创建一个Facebook测试用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆