Tutorial: Extend UniShare - Add /me API(for getting userinfo) to Facebook instance

1. Add api define.

Open Facebook.cs and find:

    const string METHOD_STATUS_SET = "status.set";
    const string METHOD_FEED = "me/feed";

add this line after above:

  const string METHOD_ME = "me";

2.Check api permission, ref:http://developers.facebook.com/docs/reference/login/#permissions

Add user_about_me to scope:

change:

    public override void Authorize(){
        fullAuthorizeUrl = string.Format("{0}?client_id={1}&response_type=token&redirect_uri={2}&scope=publish_stream", oauth.AuthorizeUrl, appKey, callbackUrl);
        base.Authorize();
        //Application.OpenURL(authUrl);
    }

to:

    public override void Authorize(){
        fullAuthorizeUrl = string.Format("{0}?client_id={1}&response_type=token&redirect_uri={2}&scope=publish_stream,user_about_me", oauth.AuthorizeUrl, appKey, callbackUrl);
        base.Authorize();
        //Application.OpenURL(authUrl);
    }

3.Add api method

    public void GetUserInfo()
    {
        Task task;
        List<HttpParameter> config = new List<HttpParameter>()
        {
//            new HttpParameter("fields", "name")
        };
        task.commandType = METHOD_ME;
        task.parameters = config;
        task.requestMethod = RequestMethod.Get;
        if(!oauth.VerifierAccessToken()){
            ResponseResult result = new ResponseResult();
            result.platformType = PlatformType.PLATFORM_FACEBOOK;
            result.returnType = ReturnType.RETURNTYPE_OAUTH_FAILED;
            result.commandType = task.commandType.ToString();
            result.description = "";
            lock(resultList){
                resultList.Add(result);
            }
        }else{
            SendCommand(task);
        }
    }    

done!

the return string is in json format looks like below:

{"id":"1254714900","name":"Peter A. Volchek","first_name":"Peter","middle_name":"A.","last_name":"Volchek","link":"http:\/\/www.facebook.com\/chhren","username":"chhren","gender":"male","timezone":8,"locale":"en_US","verified":true,"updated_time":"2012-12-04T12:47:19+0000"}

 

3. How to use:

     Facebook.instance.OnCallBack += onGetUserInfo;
     Facebook.instance.GetUserInfo();
    public void onGetUserInfo(ResponseResult res){
        Debug.Log(res.returnType.ToString());
        Debug.Log(res.description);
        if(res.returnType == ReturnType.RETURNTYPE_SUCCESS && res.commandType == Facebook.instance.METHOD_ME){
            //parse the json
            var json = JsonReader.Deserialize<Dictionary<string, object>>(res.description);
            //print user info
            Debug.Log(json["name"]);
            foreach (KeyValuePair<string ,object> item in json){
                string keyvalue = string.Format("{0}:{1}", item.Key.ToString(), item.Value.ToString());
                Debug.Log(keyvalue);
            }
        }
    }

 

 

posted @ 2013-03-02 17:06  icecryed  阅读(477)  评论(0编辑  收藏  举报