0

我正在使用affinitiz Facebook API for ColdFusion https://github.com/affinitiz/facebook-cf-sdk来开发Facebook应用程序,并且在验证后遇到了真正的事件麻烦。FB Cookie被缓存在Facebook App中

该API可以很好地抓取应用程序cookie,但我只能在模板的额外调用的帮助下重新加载,如果您愿意的话。重新加载后,所有东西都到位了,我可以通过访问令牌获得用户会话。

我已经将调用API放入了OnRequest事件的Application.cfc中。

所以这里有一件奇怪的事情,不管我从ColdFusion制作的API有多少次调用,唯一能让我获得最新App Cookie的是通过重新加载模板(我使用JavaScript来做这个工作) 。

就好像应用程序cookie以某种方式被缓存,并且仅在重新加载时刷新,尽管用户被认证。

总括来说,其过程如下:

  1. OnRequest事件
  2. API擦伤使用网站
  3. 所以我们重装
  4. OnRequest事件
  5. API擦伤使用网站
  6. 我们的Facebook UID和access_token。

这些是Application.cfc中的会话cookie设置。

<cfset THIS.SessionManagement = true /> 
<cfset THIS.SetClientCookies = true /> 
<cfset THIS.ClientManagement = true /> 

感谢您的帮助!

的OnRequest代码是相当广泛的,但这里有云:

import facebook.sdk.FacebookApp; 
    import facebook.sdk.FacebookGraphAPI; 

    // Replace this with your appId and secret 
    APP_ID = "zzz"; 
    SECRET_KEY = "zzz"; 
    API_KEY = "zzz"; 
    session.appID = APP_ID; 
    session.apiKey = API_KEY; 

    // Create facebookApp instance 
    //this.utils.runTimer("facebookAuth-newfacebookApp","start"); 
    facebookApp = new FacebookApp(appId=APP_ID, secretKey=SECRET_KEY); 
    //this.utils.runTimer("facebookAuth-newfacebookApp","stop"); 
    session.fbApp = facebookApp; 

    // We may or may not have this data based on a URL or COOKIE based session. 
    // 
    // If we get a session here, it means we found a correctly signed session using 
    // the Application Secret only Facebook and the Application know. We dont know 
    // if it is still valid until we make an API call using the session. A session 
    // can become invalid if it has already expired (should not be getting the 
    // session back in this case) or if the user authenticated out of Facebook. 

    //this.utils.runTimer("facebookAuth-getUserSession","start"); 
    userSession = facebookApp.getUserSession(); 


    //this.utils.runTimer("facebookAuth-getUserSession","stop"); 

    authenticated = false; 
    if (structKeyExists(userSession, "uid")) { 
     if(structKeyExists(session.fbUserSession,"uid")){ 
      if(userSession.uid NEQ session.fbUserSession.uid){ 
       // reset session variables; 
      } 
     } 
     session.fbUserSession = userSession; 
     try { 
      facebookGraphAPI = new FacebookGraphAPI(userSession.access_token); 
      session.fbGraphAPI = facebookGraphAPI; 

      session.fbGraphAPI = facebookGraphAPI; 

      if (NOT session.fbAuthenticated){ 
       session.fbAuthenticated = true; 
       session.justLoggedIn = true; 
      } 
      session.fbLoginCounter = 3; 
      authenticated = true; 
     } catch (any exception) { 
      // Ignore exception (OAuthInvalidTokenException), usually an invalid session 
     } finally { 

      facebookGraphAPI = new FacebookGraphAPI(); 

     } 
    } else { 

     facebookGraphAPI = new FacebookGraphAPI(); 
     session.fbUserSession = StructNew(); 
      //this.utils.runTimer("facebookAuth-FacebookGraphAPI-else","stop"); 
    } 

    session.parameters = structNew(); 
    session.parameters["req_perms"] = "publish_stream, email"; 

然后我就可以通过会话访问FB的cookie。

+1

我怀疑这里的很多人都不熟悉affinitiz Facebook API。你打电话过得怎么样?你可以显示你的onRequest代码?它返回什么? –

+0

我刚修好了。从Application.cfc的onRequest事件调用API是罪魁祸首。我的印象是,onRequest就像在所有.cfm模板上添加代码一样。 我将这些调用添加到了暴露的模板,并且一切正常。不知何故,会话cookie在onRequest中落后,甚至只能在刷新时重置。 如果有人可以解释为什么这是我会很开心:D(我将提交这个作为一个答案,只要我的潜伏者计时器是) – boethius

+0

我从来没有使用onRequest,所以我不知道任何它的问题,但我也从来没有听说过。如果你将代码放在onRequestStart()中,代码是否工作? –

回答

1

我很好奇这个节就在这里:

if(structKeyExists(session.fbUserSession,"uid")){ 
     if(userSession.uid NEQ session.fbUserSession.uid){ 
      // reset session variables; 
     } 
    } 
    session.fbUserSession = userSession; 

在第一个要求是在有任何session.fbUserSession.uid价值?我猜不是。看起来你没有设置这个值,直到之后的第3行。这是否意味着无论替换//重置会话变量会被调用?这可能会导致你一些问题?我不知道该代码做了什么,但从我所看到的情况来看,session.fbUserSession对于您的检查没有任何价值,直到至少有第二个请求。这意味着//重置会话变量代码每次都会在第一个请求上运行。那是你要的吗?

+0

只要有饼干,它就会被刮掉。 – boethius

0

我刚修复它。从Application.cfc的onRequest事件调用API是罪魁祸首。我的印象是,onRequest就像在所有.cfm模板上添加代码一样。

我将调用添加到暴露的模板,一切工作顺利。不知何故,会话cookie在onRequest中落后,甚至只能在刷新时重置。

如果任何人都可以解释为什么这是我会很开心。