好的,我一直在研究這個,以下是我想到的。
根據文檔,應該能夠通過調用在所述圖形API來創建測試用戶如下:
NSString *urlString = [NSString stringWithFormat:@"%@/accounts/test-users", kAppId];
[facebook requestWithGraphPath:urlString andParams:params andHttpMethod:@"POST" andDelegate:self];
其中params
是包含一些或所有的參數作爲在https://developers.facebook.com/docs/test_users/詳述一個NSMutableDictionary 。
然而,我發現,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];
希望這將有助於讓你在測試用戶的實驗過程中得到幫助。