javascript – 在Node.js(Express)Facebook画布应用程序中获取signed_request

前端之家收集整理的这篇文章主要介绍了javascript – 在Node.js(Express)Facebook画布应用程序中获取signed_request前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有什么方法可以在Node.js Facebook页面选项卡应用程序中获取和解析signed_request?我需要知道页面ID,如果用户喜欢页面

解决方法

我不久前做了这个,最后写了一个小库来做.最初的CoffeeScript可以在 https://gist.github.com/fbef51815ab6f062b51a#file_signed_request.coffee找到,这是一个JavaScript翻译:
var crypto = require('crypto');

SignedRequest = (function() {

  function SignedRequest(secret,request) {
    this.secret = secret;
    this.request = request;
    this.verify = this.verify.bind(this);

    var parts = this.request.split('.');
    this.encodedSignature = parts[0];
    this.encoded = parts[1];
    this.signature = this.base64decode(this.encodedSignature);
    this.decoded = this.base64decode(this.encoded);
    this.data = JSON.parse(this.decoded);
  }

  SignedRequest.prototype.verify = function() {
    if (this.data.algorithm !== 'HMAC-SHA256') {
      return false;
    }
    var hmac = crypto.createHmac('SHA256',this.secret);
    hmac.update(this.encoded);
    var result = hmac.digest('base64').replace(/\//g,'_').replace(/\+/g,'-').replace(/\=/g,'');
    return result === this.encodedSignature;
  };

  SignedRequest.prototype.base64encode = function(data) {
    return new Buffer(data,'utf8').toString('base64').replace(/\//g,'');
  };

  SignedRequest.prototype.base64decode = function(data) {
    while (data.length % 4 !== 0) {
      data += '=';
    }
    data = data.replace(/-/g,'+').replace(/_/g,'/');
    return new Buffer(data,'base64').toString('utf-8');
  };

  return SignedRequest;

})();

module.exports = SignedRequest;

您可以这样使用:

var verifier = new SignedRequest(clientSecret,signedRequest);
verifier.verify() // whether or not the signed request verifies
verifier.data // the data from the signed request
原文链接:https://www.f2er.com/js/150007.html

猜你在找的JavaScript相关文章