PHP SDK: How do I capture the access token after user auths app?

后端 未结 4 861
遥遥无期
遥遥无期 2020-12-18 16:26

This is for a canvas app on the Facebook Platform using the new(est) Facebook PHP SDK.

We are using the PHP example from the Facebook tutorial (https://developers.fa

4条回答
  •  不思量自难忘°
    2020-12-18 16:48

    OK, let's try this again.

    Server-side vs Client-side Authentication

    You are exclusively using the PHP SDK, so you want to do server-side authentication, where the authentication code is sent to the server over HTTP via the URL. This will allow you to fetch an access token for the user on the first page load after auth (in your case, the redirect page). The auth_url you are currently constructing is setting response_type=token, which forces the redirect to use client-side auth mode and set the token in the URL fragment instead of in the query. You should remove that parameter completely. In fact, I highly recommend you just use the PHP SDK instead of constructing that URL yourself. See example below.

    Application Access Tokens

    The odd-looking access token 126736467765|SECRET is your application access token, which is composed of your app ID and secret key. The application access token is returned by getAccessToken() if no user access token is available (because some API calls require at least some sort of access token). This also means that you've revealed your secret key to the world via this blog post, so you should reset your app secret otherwise anyone will be able to make API calls on your behalf. I highly recommend you elide parts of your access tokens if you share them with others.

    Token Expiration

    The OAuth 2.0 flow and v3.1.1 of the PHP SDK don't make determining the expiration time of a token all that easy. I would suggest attempting to make the API call, and then refreshing the token if the API call fails with an OAuthException. Tokens can be invalid even if they haven't expired, so this deals with more cases. However, if you still want to maintain the expiration date on your end, you might just want to extract it from the token itself. If you have an expiring token, then the expiration timestamp will be contained within that string. Here's a function I put together quickly to extract that:

    function extractExpirationFromToken($access_token) {
        $segments = explode('|', $access_token);
        if(count($segments) < 2) { return 0; }
    
        $segments = explode('.', $segments[1]);
        if(count($segments) < 4) { return 0; }
    
        $expires = $segments[3];
        $dash_pos = strrpos($expires, '-');
        if($dash_pos !== false) {
            $expires = substr($expires, 0, $dash_pos);
        }
        return $expires;
    }
    

    New Index Page Code

    // Create kk-fb app instance
    $facebook = new Facebook(array(
        'appId'  => KKFB_ID,
        'secret' => KKFB_KY,
    ));
    
    $canvas_auth = 'http://karmakorn.com/karmakorn/alpha20/kk-fb-auth.php';
    
    $auth_url = $facebook->getLoginUrl(array(
        'scope' => 'email,publish_stream',
        'redirect_uri' => $canvas_auth, // you could just redirect back to this index page though
    ));
    
    $user = $facebook->getUser();
    
    if (empty($user)) {
        echo("");
    } else {
        echo ("Welcome User: " . $user);
    }
    

    Redirect Page

    I don't think you need this page at all. You could just redirect the user back to your original index page.

    // Create kk-fb app instance
    $facebook = new Facebook(array(
        'appId'  => KKFB_ID,
        'secret' => KKFB_KY,
    ));
    
    $user = $facebook->getUser();
    $access_token = $facebook->getAccessToken();
    // also copy the function definition given earlier
    $expiration = extractExpirationFromToken($access_token);
    
    echo "User: $user 
    "; echo "Access Token: $access_token
    "; echo "Expiration: $expiration
    "; echo "Request:
    "; var_dump($_REQUEST);

提交回复
热议问题