public static <T extends FacebookSignedRequest> T getFacebookSignedRequest( String signedRequest,Class<T> clazz) throws Exception { String payload = signedRequest.split("[.]", 2)[1]; payload = payload.replace("-", "+").replace("_", "/").trim(); String jsonString = new String(Base64.decodeBase64( payload.getBytes())); return Json.fromJson(clazz, jsonString); }
signedRequest的值为:
NB1w0pvHG-BQAjiBNlnZ8TPpaQTFvfbOsa2zMKI0uk0.eyJhbGdvcml0aG0iOiJITUFDLVNIQTI1NiIsImlzc3VlZF9hdCI6MTQwNzQ1ODQ2NywidXNlciI6eyJjb3VudHJ5IjoidXMiLCJsb2NhbGUiOiJ6aF9DTiIsImFnZSI6eyJtaW4iOjIxfX19
public static T getFacebookSignedRequest<T>(String signedRequest, Class<T> clazz)
where T: class, FacebookSignedRequest
{
String payload = signedRequest.split("[.]", 2)[1];
payload = payload.replace("-", "+").replace("_", "/").trim();
String jsonString = new String(Base64.decodeBase64(
payload.getBytes()));
return Json.fromJson(clazz, jsonString);
}
上面是方法的翻译,内部的代码没打开VS,就你自己简单处理一下了,跟C#差别不大。
String payload = signedRequest.Split('.')[1]; payload = payload.Replace("-", "+").Replace("_", "/").Trim(); var jsonString = Convert.ToBase64String(Encoding.UTF8.GetBytes(payload));
我这样写的,但是得到的jsonString是一个乱七八糟的字符串,看他定义的变量名,应该是一个json字符串
@拖鞋王子:
var jsonString = Convert.ToBase64String(Encoding.UTF8.GetBytes(payload));
写错了,应该是:
var jsonString = Encoding.UTF8.GetString(Convert.FromBase64String(payload))
@519740105: 还真是