• No results found

Server-side Flow

In document Step by Step - Facebook API (Page 80-83)

User authentication and app authorization are handled at the same time by redirecting the user to ourOAuth Dialog.

When invoking this dialog, you must pass in your app id that is generated when you create your application in our Developer App(the client_id parameter) and the URL that the user's browser will be redirected back to once app authorization is completed (the redirect_uri parameter). The redirect_uri must be in the path of the Site URL you specify in Website section of the Summary tab in the Developer App. Note, your redirect_uri can not be a redirector.

https://www.facebook.com/dialog/oauth?

client_id=YOUR_APP_ID&redirect_uri=YOUR_URL

See theAlternate Redirect URIssection below for more information.

If the user is already logged in, we validate the login cookie that we have stored on the user's browser, authenticating the user. If the user is not logged in, they are prompted to enter their credentials:

Dokumentasi Dukungan Blog Aplikasi Search Documentation / Apps

Once we have successfully authenticated the user, the OAuth Dialog will prompt the user to authorize the app:

By default, the user is asked to authorize the app to access basic information that is available publicly or by default on Facebook. If your app needs more than this basic information to function, you must request specific permissions from the user. This is accomplished by adding a scope parameter to the OAuth Dialog request followed by comma separated list of the required permissions. The following example shows how to ask for access to user's email address and their news feed:

https://www.facebook.com/dialog/oauth?

client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream This results in the following dialog being presented to the user after they are authenticated:

A full list of permissions is available in ourpermissions reference. There is a strong inverse correlation between the number of permissions your app requests and the number of users that will allow those permissions. The greater the number of permissions you ask for, the lower the number of users that will grant them; so we recommend that you only request the permissions you absolutely need for your app.

If the user presses Don't Allow, your app is not authorized. The OAuth Dialog will redirect (via HTTP 302) the user's browser to the URL you passed in the redirect_uri parameter with the following error information:

http://YOUR_URL?error_reason=user_denied&

error=access_denied&error_description=The+user+denied+your+request.

If the user presses Allow, your app is authorized. The OAuth Dialog will redirect (via HTTP 302) the user's browser to the URL you passed in the redirect_uri parameter with an authorization code:

http://YOUR_URL?code=A_CODE_GENERATED_BY_SERVER

With this code in hand, you can proceed to the next step, app authentication, to gain the access token you need to make API calls.

In order to authenticate your app, you must pass the authorization code and your app secret to the Graph API token endpoint - along with the exact same redirect_uri used above - at

https://graph.facebook.com/oauth/access_token. The app secret is available from theDeveloper Appand should not be shared with anyone or embedded in any code that you will distribute (you should use the client-side flow for these scenarios).

https://graph.facebook.com/oauth/access_token?

client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&

client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE

If your app is successfully authenticated and the authorization code from the user is valid, the authorization server will return the access token:

In addition to the access token (the access_token parameter), the response contains the number of seconds until the token expires (the expires parameter). Once the token expires, you will need to re-run the steps above to generate a new code and access_token, although if the user has already authorized your app, they will not be prompted to do so again. If your app needs an access token with an infinite expiry time (perhaps to take actions on the user's behalf after they are not using your app), you can request the offline_access permission.

If there is an issue authenticating your app, the authorization server will issue an HTTP 400 and return the error in the body of the response:

{

"error": {

"type": "OAuthException",

"message": "Error validating verification code."

} }

The diagram below illustrates the HTTP calls made through the server-side flow:

The following PHP example demonstrates the server-side flow withCSRF protectionin one self-contained example:

<?php

$app_id = "YOUR_APP_ID";

$app_secret = "YOUR_APP_SECRET";

$my_url = "YOUR_URL";

session_start();

$code = $_REQUEST["code"];

if(empty($code)) {

$_SESSION['state'] = md5(uniqid(rand(), TRUE)); //CSRF protection

$dialog_url = "https://www.facebook.com/dialog/oauth?client_id="

. $app_id . "&redirect_uri=" . urlencode($my_url) . "&state="

. $_SESSION['state'];

echo("<script> top.location.href='" . $dialog_url . "'</script>");

}

if($_REQUEST['state'] == $_SESSION['state']) {

$token_url = "https://graph.facebook.com/oauth/access_token?"

. "client_id=" . $app_id . "&redirect_uri=" . urlencode($my_url) . "&client_secret=" . $app_secret . "&code=" . $code;

$response = @file_get_contents($token_url);

$params = null;

parse_str($response, $params);

$graph_url = "https://graph.facebook.com/me?access_token="

. $params['access_token'];

$user = json_decode(file_get_contents($graph_url));

echo("Hello " . $user->name);

} else {

echo("The state does not match. You may be a victim of CSRF.");

}

?>

In document Step by Step - Facebook API (Page 80-83)

Related documents