You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

235 lines
7.9 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Sog;
//using Sog.Json;
using LitJson;
using ProtoCSStruct;
namespace HttpProxy
{
public class FacebookAuth
{
private static readonly string BaseUrl = "https://graph.facebook.com";
private static string AppID;
private static string AppSecret;
private static string AuthUrl;
public static void Init(string id,string secret)
{
AppID = id;
AppSecret = secret;
AuthUrl = BaseUrl + "/debug_token";
}
/// <summary>
/// 返回验证成功后,如果验证失败返回空
/// </summary>
/// <param name="accountid"></param>
/// <param name="token"></param>
/// <param name="exception">是否Facebook服务器异常</param>
/// <returns></returns>
public static bool AuthToken(string accountid, string token, out bool exception)
{
string input_token = token;
string access_token = AppID + "|" + AppSecret;
//成功,打印一下日志
TraceLog.Debug("FacebookAuth.AuthToken, start accountid {0}", accountid);
string fullUrl = string.Format("{0}?input_token={1}&access_token={2}", AuthUrl, input_token, access_token);
List<KeyValuePair<String, String>> contentParams = new List<KeyValuePair<string, string>>();
contentParams.Add(new KeyValuePair<string, string>("input_token", input_token));
contentParams.Add(new KeyValuePair<string, string>("access_token", access_token));
string strRet = HttpUtils.HttpGet(fullUrl, out exception, null);
if (strRet == null)
{
TraceLog.Error("FacebookAuth.AuthToken, HttpGet return null query failed");
return false;
}
LitJson.JsonData jsonData = JsonMapper.ToObject(strRet);
LitJson.JsonData data = jsonData["data"];
if (data == null)
{
TraceLog.Error("FacebookAuth.AuthToken, ret {0}, no data", strRet);
return false;
}
LitJson.JsonData app_id = data["app_id"];
LitJson.JsonData is_valid = data["is_valid"];
LitJson.JsonData user_id = data["user_id"];
if (app_id == null || is_valid == null || user_id == null)
{
TraceLog.Error("FacebookAuth.AuthToken ,ret {0}, no valid data,app_id,is_valid,user_id", strRet);
return false;
}
if (app_id.ToString() != AppID)
{
TraceLog.Error("FacebookAuth.AuthToken ,ret {0}, appid invalid {1}", strRet, app_id.ToString());
return false;
}
if (is_valid.ToString().ToLower() != "true")
{
TraceLog.Error("FacebookAuth.auth ,ret {0}, is_valid invalid {1}", strRet, is_valid.ToString());
return false;
}
if (user_id.ToString() != accountid)
{
TraceLog.Error("FacebookAuth.AuthToken ,ret {0}, user_id {1} != req accountid {2}", strRet, user_id.ToString(), accountid);
return false;
}
//成功,打印一下日志
TraceLog.Debug("FacebookAuth.AuthToken ,ret {0}, accountid {1}", strRet, accountid);
return true;
}
//返回name,gender,picture_url
public static string[] QueryUserInfo(string token, out bool exception)
{
string fullUrl = string.Format("{0}/me?scope=email&fields=id,name,email,link,picture,gender&access_token={1}"
, BaseUrl, token);
string strRet = HttpUtils.HttpGet(fullUrl, out exception, null);
if (strRet == null)
{
TraceLog.Error("FacebookAuth.QueryUserInfo ,HttpGet return null query failed");
return null;
}
LitJson.JsonData jsonData = JsonMapper.ToObject(strRet);
if (jsonData == null)
{
TraceLog.Error("FacebookAuth.QueryUserInfo ,ret {0}, no data", strRet);
return null;
}
LitJson.JsonData id = jsonData["id"];
LitJson.JsonData name = jsonData["name"];
//gender不一定存在
LitJson.JsonData gender = jsonData["gender"];
//email不一定存在
LitJson.JsonData email = jsonData["email"];
if (id == null || name == null)
{
TraceLog.Error("FacebookAuth.QueryUserInfo ,ret {0}, no valid data,id,name", strRet);
return null;
}
//其实返回id就可以了,这个url可以客户端拼接
string picture_url = string.Format("{0}/{1}/picture?type=large", BaseUrl, id.ToString());
TraceLog.Debug("FacebookAuth.QueryUserInfo ,ret {0}", strRet);
string strGender = "male";
if(gender != null)
{
strGender = gender.ToString();
}
string strEmail = "";
if(email != null)
{
strEmail = email.ToString();
}
string[] result = new string[4];
result[0] = name.ToString();
result[1] = strGender;
result[2] = picture_url.ToString();
result[3] = strEmail;
return result;
}
public static List<SnsFriendInfoAccount> QuerySnsFriendList(string accountToken, out bool exception)
{
string queryUrl = string.Format("{0}/me/friends?access_token={1}", BaseUrl, accountToken);
List<SnsFriendInfoAccount> list = new List<SnsFriendInfoAccount>();
QuerySnsFriendListOneUrl(queryUrl, list, out exception);
TraceLog.Debug("FacebookAuth.QuerySnsFriendList end, friend count {0}", list.Count);
return list;
}
private static void QuerySnsFriendListOneUrl(string queryUrl, List<SnsFriendInfoAccount> list, out bool exception)
{
string strRet = HttpUtils.HttpGet(queryUrl, out exception, null);
if (strRet == null)
{
TraceLog.Error("FacebookAuth.QuerySnsFriendListOneUrl ,HttpGet return null query failed");
return ;
}
TraceLog.DebugNoFormat(strRet);
LitJson.JsonData jsonData = JsonMapper.ToObject(strRet);
if (jsonData == null)
{
TraceLog.Error("FacebookAuth.QuerySnsFriendListOneUrl ,ret {0}, no data", strRet);
return ;
}
LitJson.JsonData data = jsonData["data"];
if (data != null)
{
for (int i = 0; i < data.Count; i++)
{
LitJson.JsonData friend_data = data[i];
SnsFriendInfoAccount snsFriend = new SnsFriendInfoAccount();
snsFriend.AccountType = 1;
LitJson.JsonData id = friend_data["id"];
LitJson.JsonData name = friend_data["name"];
if (id != null && name != null)
{
snsFriend.AccountID.SetString( id.ToString());
snsFriend.Nick.SetString( name.ToString());
list.Add(snsFriend);
}
}
//是不是有paging.next就一定要继续查询呢
LitJson.JsonData paging = jsonData["paging"];
if (paging != null)
{
LitJson.JsonData next = paging["next"];
if (next != null)
{
//递归调用下次查询
string nextUrl = next.ToString();
QuerySnsFriendListOneUrl(nextUrl, list, out exception);
}
}
}
}
}
}