Skip to content

Commit da1534e

Browse files
committed
User Access Tokens with Facebook Graph API
1 parent 959d086 commit da1534e

3 files changed

Lines changed: 137 additions & 0 deletions

File tree

facebook_graph_api/defines.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
// fb defines
3+
define( 'FB_APP_ID', '{FB-APP-ID}' );
4+
define( 'FB_APP_SECRET', '{FB-APP-SECRET}' );
5+
define( 'FB_REDIRECT_URI', '{REDIRECT-URI}' );
6+
define( 'FB_GRAPH_VERSION', 'v12.0' );
7+
define( 'FB_GRAPH_DOMAIN', 'https://graph.facebook.com/' );

facebook_graph_api/functions.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Make a a curl call to an endpoint with params
4+
*
5+
* @param string $endpoint we are hitting
6+
* @param string $type of request
7+
* @param array $params to send along with the request
8+
*
9+
* @return array with the api response
10+
*/
11+
function makeApiCall( $endpoint, $type, $params ) {
12+
// initialize curl
13+
$ch = curl_init();
14+
15+
// create endpoint with params
16+
$apiEndpoint = $endpoint . '?' . http_build_query( $params );
17+
18+
// set other curl options
19+
curl_setopt( $ch, CURLOPT_URL, $apiEndpoint );
20+
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
21+
22+
// get response
23+
$response = curl_exec( $ch );
24+
25+
// close curl
26+
curl_close( $ch );
27+
28+
return array( // return data
29+
'type' => $type,
30+
'endpoint' => $endpoint,
31+
'params' => $params,
32+
'api_endpoint' => $apiEndpoint,
33+
'data' => json_decode( $response, true )
34+
);
35+
}
36+
37+
/**
38+
* Get facebook api login url that will take the user to facebook and present them with login dialog.
39+
*
40+
* Endpoint: https://www.facebook.com/{fb-graph-api-version}/dialog/oauth?client_id={app-id}&redirect_uri={redirect-uri}&state={state}&scope={scope}&auth_type={auth-type}
41+
*
42+
* @param string $scope comma separated list of permissions being requested from the user..
43+
* @param string $state random generated to verify request is from facebook.
44+
* @return string
45+
*/
46+
function getFacebookLoginUrl( $permissions, $state ) {
47+
// endpoint for facebook login dialog
48+
$endpoint = 'https://www.facebook.com/' . FB_GRAPH_VERSION . '/dialog/oauth';
49+
50+
$params = array( // login url params required to direct user to facebook and promt them with a login dialog
51+
'client_id' => FB_APP_ID,
52+
'redirect_uri' => FB_REDIRECT_URI,
53+
'state' => $state,
54+
'scope' => $permissions,
55+
'auth_type' => 'rerequest'
56+
);
57+
58+
// return login url
59+
return $endpoint . '?' . http_build_query( $params );
60+
}
61+
62+
/**
63+
* Get an access token with the code from facebook.
64+
*
65+
* Endpoint https://graph.facebook.com/{fb-graph-version}/oauth/access_token?client_id{app-id}&client_secret={app-secret}&redirect_uri={redirect_uri}&code={code}
66+
*
67+
* @param string $code code returned from facebook, exchange for access token
68+
* @return array $response
69+
*/
70+
function getAccessTokenWithCode( $code ) {
71+
// endpoint for getting an access token with code
72+
$endpoint = FB_GRAPH_DOMAIN . FB_GRAPH_VERSION . '/oauth/access_token';
73+
74+
$params = array( // params for the endpoint
75+
'client_id' => FB_APP_ID,
76+
'client_secret' => FB_APP_SECRET,
77+
'redirect_uri' => FB_REDIRECT_URI,
78+
'code' => $code
79+
);
80+
81+
// make the api call
82+
return makeApiCall( $endpoint, 'GET', $params );
83+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
// include our defines file
3+
include 'defines.php';
4+
5+
// include php functions
6+
include 'functions.php';
7+
8+
// allow us to user php session
9+
session_start();
10+
11+
if ( isset( $_GET['logout'] ) ) { // log user out
12+
// clea session
13+
unset( $_SESSION );
14+
15+
// refresh page
16+
header( 'get_user_access_token.php' );
17+
}
18+
19+
if ( !isset( $_SESSION['fb_access_token'] ) && isset( $_GET['code'] ) && isset( $_GET['state'] ) && $_GET['state'] == $_SESSION['fb_state'] ) { // we have get vars from facebook
20+
// get access token and setit in the session
21+
$accessToken = getAccessTokenWithCode( $_GET['code'] );
22+
$_SESSION['fb_access_token'] = $accessToken['data'];
23+
} elseif ( !isset( $_SESSION['fb_access_token'] ) ) {
24+
// create state for fb
25+
$_SESSION['fb_state'] = mt_rand( 1, 1000000 );
26+
27+
// get fb login url
28+
$fbLoginUrl = getFacebookLoginUrl( 'email,public_profile', $_SESSION['fb_state'] );
29+
}
30+
?>
31+
<h1>Facebook Graph API Get User Access Token</h1>
32+
<hr />
33+
<h3>$_SESSION</h3>
34+
<pre><?php print_r( $_SESSION ); ?></pre>
35+
<hr />
36+
<?php if ( isset( $fbLoginUrl ) ) : // need to display login with facebook to user ?>
37+
<a href="<?php echo $fbLoginUrl; ?>">
38+
Login With Facebook
39+
</a>
40+
<br />
41+
<br />
42+
href: <?php echo $fbLoginUrl; ?>
43+
<?php else : // user is logged so show logout ?>
44+
<a href="get_user_access_token.php?logout=1">
45+
Logout
46+
</a>
47+
<?php endif; ?>

0 commit comments

Comments
 (0)