Facebook OAuth: custom callback_uri parameters

前端 未结 6 1903
广开言路
广开言路 2020-12-23 14:34

I\'d like to have a dynamic redirect URL for my Facebook OAuth2 integration. For example, if my redirect URL is this in my Facebook app:

http://www.mysite.com/oau         


        
相关标签:
6条回答
  • 2020-12-23 14:52

    I was trying to implement a Facebook login workflow against API v2.9 following this tutorial. I tried the solutions described above. Manuel's answer is sort of correct, but what I observed is url encoding is not needed. Plus, you can only pass one parameter. Only the first query parameter will be considered, the rest will be ignored. Here is an example,

    1. Request a code via https://www.facebook.com/v2.9/dialog/oauth?client_id={app-id}&redirect_uri=http://{url}/login-redirect?myExtraParameter={some-value}

    2. You'd get a callback for your url. It will look like http://{url}/login-redirect?code={code-from-facebook}&myExtraParameter={value-passed-in-step-1}. Note that facebook would make a callback with myExtraParameter. You can extract the value for myExtraParameter from callback url.

    3. Then you can request access token with https://graph.facebook.com/v2.9/oauth/access_token?client_id={app-id}&client_secret={app-secret}&code={code-from-facebook}&redirect_uri=http://{url}/login-redirect?myExtraParameter={value-extracted-in-step-2}

    Additional parameter passed in step 1 after the first query parameter will be ignored. Also make sure to not include any invalid characters in your query parameter (see this for more information).

    0 讨论(0)
  • 2020-12-23 15:03

    I figured out the answer; rather than adding additional parameters to the redirect URL, you can add a state parameter to the request to https://www.facebook.com/dialog/oauth:

    https://www.facebook.com/dialog/oauth
        ?client_id=MY_CLIENT_ID
        &scope=MY_SCOPE
        &redirect_uri=http%3A%2F%2Fwww.mysite.com%2Foauth_callback%3Ffoo%3Dbar
        &state=6234
    

    That state parameter is then passed to the callback URL.

    0 讨论(0)
  • 2020-12-23 15:08

    You should set your custom state parameter using the login helper as such:

    use Facebook\Facebook;
    use Illuminate\Support\Str;
    
    $fb = new Facebook([
        'app_id' => env('FB_APP_ID'),
        'app_secret' => env('FB_APP_SECRET'),
        'default_graph_version' => env('FB_APP_VER'),
    ]);
    
    $helper = $fb->getRedirectLoginHelper();
    
    $permissions = [
        'public_profile',
        'user_link',
        'email',
        'read_insights',
        'pages_show_list',
        'instagram_basic',
        'instagram_manage_insights',
        'manage_pages'
    ];
    
    $random = Str::random(20);
    
    $OAuth2Client = $fb->getOAuth2Client();
    
    $redirectLoginHelper = $fb->getRedirectLoginHelper();
    
    $persistentDataHandler = $redirectLoginHelper->getPersistentDataHandler();
    
    $persistentDataHandler->set('state', $random);
    
    $loginUrl = $OAuth2Client->getAuthorizationUrl(
            url('/') . '/auth/facebook',
            $random,
            $permissions
        );
    
    0 讨论(0)
  • 2020-12-23 15:08

    You're best off specifying a unique callback for each oAuth provider, /oauth/facebook, /oauth/twitter etc.

    If you really want the same file to respond to all oAuth requests, either include it in the individual files or setup a path that will call the same file on your server using .htaccess redirects or something similar: /oauth/* > oauth_callback.ext

    0 讨论(0)
  • 2020-12-23 15:11

    If, for any reason, you can't use the option that Jacob suggested as it's my case, you can urlencode your redirect_uri parameter before passing it and it will work, even with a complete querystring like foo=bar&morefoo=morebar in it.

    0 讨论(0)
  • 2020-12-23 15:14

    Hey if you are using official facebook php skd then you can set custom state param like this

    $helper = $fb->getRedirectLoginHelper();
    $helper->getPersistentDataHandler()->set('state',"any_data");
    $url = $helper->getLoginUrl($callback_url, $fb_permissions_array);
    
    0 讨论(0)
提交回复
热议问题