0

当你注册一个 facebook 应用程序时,你会得到

应用程序ID:123455678 应用程序密钥:hkjhkh3434hkklljk 应用程序秘密:jkjljlj1233455jk

对于 OAuth 2,只有应用程序 ID(又名 client_id)和应用程序机密(又名 client_secret)是有用的。

想知道应用程序密钥的用途是什么?是出于某种后端目的吗?如果是,那么暴露的意义何在。

4

1 回答 1

2

我只是在这里大声思考。

我想这只是为了向后兼容,特别是对于旧的Facebook Connect实现和使用 REST API APP_KEY

正如您在FB.initJavascript-SDK 中看到的:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId  : 'YOUR APP ID',
      status : true, // check login status
      cookie : true, // enable cookies to allow the server to access the session
      xfbml  : true  // parse XFBML
    });
  };

  (function() {
    var e = document.createElement('script');
    e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
    e.async = true;
    document.getElementById('fb-root').appendChild(e);
  }());
</script>

他们没有提到与NEW PHP-SDKapiKey一起使用的代码。 现在,如果您转到旧的connect-js 示例

FB.init({ apiKey: '48f06bc570aaf9ed454699ec4fe416df' });

所以调试connect.facebook.net/en_US/all.js文件(使用JSBeautifier):

FB.provide('', {
    init: function (a) {
        a = FB.copy(a || {}, {
            logging: true,
            status: true
        });
        FB._apiKey = a.appId || a.apiKey;
        if (!a.logging && window.location.toString().indexOf('fb_debug=1') < 0) FB._logging = false;
        FB.XD.init(a.channelUrl);
        if (FB._apiKey) {
            FB.Cookie.setEnabled(a.cookie);
            a.session = a.session || FB.Cookie.load();
            FB.Auth.setSession(a.session, a.session ? 'connected' : 'unknown');
            if (a.status) FB.getLoginStatus();
        }
        if (a.xfbml) window.setTimeout(function () {
            if (FB.XFBML) FB.Dom.ready(FB.XFBML.parse);
        }, 0);
    }
});

您可以在此处看到它正在检查apiIdor的存在,apiKey然后尝试调用图形 api 和其余 api

FB.provide('', {
    api: function () {
        if (typeof arguments[0] === 'string') {
            FB.ApiServer.graph.apply(FB.ApiServer, arguments);
        } else FB.ApiServer.rest.apply(FB.ApiServer, arguments);
    }
});

和:

graph: function () {
    var a = Array.prototype.slice.call(arguments),
        f = a.shift(),
        d = a.shift(),
        c, e, b;
    while (d) {
        var g = typeof d;
        if (g === 'string' && !c) {
            c = d.toLowerCase();
        } else if (g === 'function' && !b) {
            b = d;
        } else if (g === 'object' && !e) {
            e = d;
        } else {
            FB.log('Invalid argument passed to FB.api(): ' + d);
            return;
        }
        d = a.shift();
    }
    c = c || 'get';
    e = e || {};
    if (f[0] === '/') f = f.substr(1);
    if (FB.Array.indexOf(FB.ApiServer.METHODS, c) < 0) {
        FB.log('Invalid method passed to FB.api(): ' + c);
        return;
    }
    FB.ApiServer.oauthRequest('graph', f, c, e, b);
},
rest: function (e, a) {
    var c = e.method.toLowerCase().replace('.', '_');
    if (FB.Auth && c === 'auth_revokeauthorization') {
        var d = a;
        a = function (f) {
            if (f === true) FB.Auth.setSession(null, 'notConnected');
            d && d(f);
        };
    }
    e.format = 'json-strings';
    e.api_key = FB._apiKey;
    var b = FB.ApiServer._readOnlyCalls[c] ? 'api_read' : 'api';
    FB.ApiServer.oauthRequest(b, 'restserver.php', 'get', e, a);
},

正如您在此处看到的,它与Old Rest API一起使用,阅读那里的文档:

REST API 支持 OAuth 2.0 以及较旧的自定义授权签名方案。有关如何将现有会话升级到 OAuth 2.0 的信息,请参阅身份验证升级指南。

所以APP_KEY绝对是为了向后兼容!

于 2011-02-12T11:22:57.503 回答