简体   繁体   English

从我的应用程序访问Facebook画布URL参数

[英]Access facebook canvas URL parameters from my app

I'm making a facebook canvas app (a game) and I want my players to use links with url parameters that link to different areas in the game, ie 我正在制作一个Facebook canvas应用(一个游戏),我希望我的玩家使用带有url参数的链接,这些链接链接到游戏中的不同区域,即

https://apps.facebook.com/mgatlandtestapp?level=emeraldhillzone https://apps.facebook.com/mgatlandtestapp?level=emeraldhillzone

How do I access the level=emeraldhillzone information in my app (either client-side with javascript in an iFrame, or server-side as a node.js application) 如何在我的应用程序中访问level = emeraldhillzone信息(客户端在iFrame中使用javascript,或者在服务器端作为node.js应用程序)


Things I've tried so far: 到目前为止我尝试过的事情:

  1. This 4-year-old answer says it's possible to in the client-side code but doesn't say how (sorry if I'm missing something obvious! window.location gives the location of my heroku server, not the facebook url.) 这个已有4年历史的答案指出,可以在客户端代码中进行操作,但没有说明如何操作(对不起,如果我缺少明显的内容!window.location提供了我的heroku服务器的位置,而不是facebook的URL。)

  2. Some of the facebook documentation made me think this would work: 一些Facebook文档使我认为这可行:

Use a url in the form https://apps.facebook.com/mgatlandtestapp?app_data=yourDataHere then on the server side get the signed request sent to the server and it will contain an app_data field. 使用形式为https://apps.facebook.com/mgatlandtestapp?app_data=yourData的url,然后在服务器端将已签名的请求发送到服务器,其中将包含一个app_data字段。

However when I did this, the signed request did not contain an app_data field. 但是,当我这样做时,已签名的请求不包含app_data字段。

After reading more, I think the app_data thing only works if the app is being loaded within a facebook Page Tab, not a facebook Canvas Page. 阅读更多内容后,我认为只有在Facebook Page Tab(而不是Facebook Canvas Page)中加载应用程序时,app_data才起作用。

Facebook sends an initial POST to your app, which includes the url parameters. Facebook将初始POST发送到您的应用,其中包括url参数。 You can access the url parameters from in that POST request. 您可以从该POST请求中访问url参数。

I used this url to load the page: 我使用此网址加载页面:

https://apps.facebook.com/mgatlandtest/?level=emeraldhillzone

I used this code in my node.js server app to serve the request 我在node.js服务器应用程序中使用了此代码来处理请求

app.post('/*', function(request, response) {
  console.log('url parameters:');
  console.log(request.query);
  response.redirect('/');
});

The output in the node console was: 节点控制台中的输出为:

url parameters: 网址参数:
{ level: 'emeraldhillzone' } {等级:'emeraldhillzone'}

You could then use the parameters to redirect to a different page. 然后,您可以使用参数重定向到其他页面。


This was actually really simple, once I got past all the distracting misinformation. 一旦我克服了所有分散注意力的错误信息,这实际上非常简单。 The key was searching for 'deep linking' (instead of 'url parameters'), which brought up this answer 关键是要搜索“深层链接”(而不是“ url参数”),从而得出这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM