简体   繁体   English

Facebook access_token无效

[英]Facebook access_token not valid

I am building a little website for an event. 我正在为活动建立一个小网站。 The idea is to add the attending persons from the facebook event to this website. 想法是将来自Facebook活动的参与者添加到该网站。 The problem is that the attending service needs an access_token. 问题在于参加服务需要一个access_token。 I don't want a user to log in to the app, I just want to use the app access_token. 我不希望用户登录到应用程序,我只想使用应用程序access_token。 But this doesn't seem to work. 但这似乎不起作用。

I have the following php code for receiving the access_token: 我有以下用于接收access_token的php代码:

// get facebook auth_token
$FB_APP_ID = '****';
$FB_APP_SECRET = '***';
$token_url = "https://graph.facebook.com/oauth/access_token?client_id=" . $FB_APP_ID . "&client_secret=" . $FB_APP_SECRET . "&grant_type=client_credentials";
$app_token = file_get_contents($token_url);
parse_str($app_token); // creates: $access_token

In javascript I am using the following code: 在javascript中,我使用以下代码:

/**
 * Gets all the attending Users
 * @param function callback: the function to call, when data loading is ready
 * @param function callback: the function to call, when data loading is broken
 * @return void
 */
this.getAttending = function(callback, errorHandler) 
{
    FB.api('/' + this.eventId + '/attending', function(response) {
        if(response.error) {
            errorHandler(response.error['message']);
        } else {
            callback(response.data);
        }
    } , {access_token: facebook.accessToken} );    
}

I receive the error: Invalid OAuth access token. 我收到错误:无效的OAuth访问令牌。

The access token looks like: 111737995591905|l9e3niEMM1RsIUhwHZv3pn3c19M 访问令牌看起来像:111737995591905 | l9e3niEMM1RsIUhwHZv3pn3c19M

THe user access token looks like: 145634995501895|2.AQBcWfbvdzhloLYc.3600.1312138800.1-1146858041|cjCkHZqquyyFyX2dY0q2YCaSyy0 用户访问令牌看起来像:145634995501895 | 2.AQBcWfbvdzhloLYc.3600.1312138800.1-1146858041 | cjCkHZqquyyFyX2dY0q2YCaSyy0

When i try to use the user access token, everything works fine. 当我尝试使用用户访问令牌时,一切正常。 But when I hardcode the token, this will not work for other users. 但是,当我对令牌进行硬编码时,这将不适用于其他用户。 When I try the token that the server parsed for me, I get die invalid token action. 当我尝试服务器为我解析的令牌时,我得到无效的令牌动作。 Does somebody knows a solution for this? 有人知道解决方案吗? It is a public event, and I don't want the user to login before he/she knows who is attending? 这是一个公共活动,我不希望用户在知道谁在参加之前先登录。

Is this possible at all? 这有可能吗? Thanx! 谢谢!

Why not get an access token for yourself and then hard code that in your application (assuming it is running on a secure server where no one else can steal the credentials). 为什么不为自己获取访问令牌,然后在应用程序中对其进行硬编码(假设它运行在安全的服务器上,没有其他人可以窃取凭据)。 Then anyone visiting the page can view the guest list using your own access token which is permissioned to view the list. 这样,访问该页面的任何人都可以使用您自己的访问令牌(有权查看该列表)查看来宾列表。

The client_credentials grant type is not meant for accessing user data, only application data. client_credentials授予类型并不用于访问用户数据,仅用于访问应用程序数据。 I don't know how the rest of the application works but I'm assuming that you need a user access token to see user data. 我不知道应用程序其余部分的工作方式,但我假设您需要一个用户访问令牌才能查看用户数据。

Did you encode the token while hard coding? 您在硬编码时是否对令牌进行编码? | | should be %7C . 应该是%7C。 Cant think of anything else that might have gone wrong for the moment 无法想到目前可能出错的任何其他内容

The way i got around this was designating the offline_access permission, it generates an access token that never expires so you can use it indefinitely. 我解决此问题的方法是指定offline_access权限,它会生成一个永不过期的访问令牌,因此您可以无限期地使用它。 You would then in theory authorize only your Facebook account to have the event data pulled from it. 从理论上讲,您将只授权您的Facebook帐户从中提取事件数据。 You could then view and serve your event details to your website no problem. 然后,您可以毫无问题地查看事件详细信息并将其提供给您的网站。

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

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