読み取りアクセスのみでユーザー権限を取得する
let permissionsDesired = ["email", "public_profile", "user_friends"]
let facebookLogin = FBSDKLoginManager()
facebookLogin.logInWithReadPermissions(permissionsDesired,
fromViewController: sender)
{ (facebookLogin:FBSDKLoginManagerLoginResult!, facebookError:NSError!) -> Void in
if facebookError != nil
{
print("Facebook login. Error: \(facebookError.localizedDescription)")
self.delegate?.permissionResults(false)
}
else
{
// access granted, you can start
//requesting user data now
self.requestUserInfo()
}
}
以下は、ユーザーに権限を付与した後にユーザーデータを取得する方法です。
func requestUserInfo( )
{
let graphPath:String = "me"
let httpMethod:String = "GET"
var listOfrequest:String = String("about")
listOfrequest = listOfrequest .stringByAppendingString(",age_range")
listOfrequest = listOfrequest .stringByAppendingString(",bio")
listOfrequest = listOfrequest .stringByAppendingString(",birthday")
listOfrequest = listOfrequest .stringByAppendingString(",context")
listOfrequest = listOfrequest .stringByAppendingString(",cover")
listOfrequest = listOfrequest .stringByAppendingString(",currency")
listOfrequest = listOfrequest .stringByAppendingString(",devices")
listOfrequest = listOfrequest .stringByAppendingString(",education")
listOfrequest = listOfrequest .stringByAppendingString(",email")
listOfrequest = listOfrequest .stringByAppendingString(",favorite_athletes")
listOfrequest = listOfrequest .stringByAppendingString(",favorite_teams")
listOfrequest = listOfrequest .stringByAppendingString(",first_name")
listOfrequest = listOfrequest .stringByAppendingString(",gender")
listOfrequest = listOfrequest .stringByAppendingString(",hometown")
listOfrequest = listOfrequest .stringByAppendingString(",id")
listOfrequest = listOfrequest .stringByAppendingString(",inspirational_people")
listOfrequest = listOfrequest .stringByAppendingString(",installed")
listOfrequest = listOfrequest .stringByAppendingString(",install_type")
listOfrequest = listOfrequest .stringByAppendingString(",is_shared_login")
listOfrequest = listOfrequest .stringByAppendingString(",is_verified")
listOfrequest = listOfrequest .stringByAppendingString(",languages")
listOfrequest = listOfrequest .stringByAppendingString(",last_name")
listOfrequest = listOfrequest .stringByAppendingString(",link")
listOfrequest = listOfrequest .stringByAppendingString(",locale")
listOfrequest = listOfrequest .stringByAppendingString(",location")
listOfrequest = listOfrequest .stringByAppendingString(",meeting_for")
listOfrequest = listOfrequest .stringByAppendingString(",middle_name")
listOfrequest = listOfrequest .stringByAppendingString(",name")
listOfrequest = listOfrequest .stringByAppendingString(",name_format")
listOfrequest = listOfrequest .stringByAppendingString(",payment_pricepoints")
listOfrequest = listOfrequest .stringByAppendingString(",picture")
listOfrequest = listOfrequest .stringByAppendingString(",political")
listOfrequest = listOfrequest .stringByAppendingString(",public_key")
listOfrequest = listOfrequest .stringByAppendingString(",quotes")
listOfrequest = listOfrequest .stringByAppendingString(",relationship_status")
listOfrequest = listOfrequest .stringByAppendingString(",religion")
listOfrequest = listOfrequest .stringByAppendingString(",security_settings")
listOfrequest = listOfrequest .stringByAppendingString(",shared_login_upgrade_required_by")
listOfrequest = listOfrequest .stringByAppendingString(",significant_other")
listOfrequest = listOfrequest .stringByAppendingString(",sports")
listOfrequest = listOfrequest .stringByAppendingString(",test_group")
listOfrequest = listOfrequest .stringByAppendingString(",third_party_id")
listOfrequest = listOfrequest .stringByAppendingString(",timezone")
listOfrequest = listOfrequest .stringByAppendingString(",updated_time")
listOfrequest = listOfrequest .stringByAppendingString(",verified")
listOfrequest = listOfrequest .stringByAppendingString(",video_upload_limits")
listOfrequest = listOfrequest .stringByAppendingString(",viewer_can_send_gift")
listOfrequest = listOfrequest .stringByAppendingString(",website")
listOfrequest = listOfrequest .stringByAppendingString(",work")
let params:[NSObject:AnyObject] = ["fields": listOfrequest ]
let graphRequest : FBSDKGraphRequest = FBSDKGraphRequest(graphPath: graphPath,
parameters: params,
HTTPMethod: httpMethod)
graphRequest.startWithCompletionHandler(
{ (connection:FBSDKGraphRequestConnection!, userData:AnyObject!, error:NSError!) -> Void in
if (error == nil)
{
print(userData)
}
else
{
print("Error: \(error.localizedDescription)")
//Handle error here
}
})
}//eom