Skip to content

Commit 7ab6d00

Browse files
committed
facebook graph api page access tokens
1 parent da1534e commit 7ab6d00

3 files changed

Lines changed: 60 additions & 2 deletions

File tree

facebook_graph_api/defines.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,9 @@
44
define( 'FB_APP_SECRET', '{FB-APP-SECRET}' );
55
define( 'FB_REDIRECT_URI', '{REDIRECT-URI}' );
66
define( 'FB_GRAPH_VERSION', 'v12.0' );
7-
define( 'FB_GRAPH_DOMAIN', 'https://graph.facebook.com/' );
7+
define( 'FB_GRAPH_DOMAIN', 'https://graph.facebook.com/' );
8+
9+
$userAccessToken = '{FB-USER-ACCESS-TOKEN}';
10+
11+
$pageId = '{FB-PAGE-ID}';
12+
$pageAccessToken = '{FB-PAGE-ACCESS-TOKEN}';

facebook_graph_api/functions.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function getFacebookLoginUrl( $permissions, $state ) {
6565
* 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}
6666
*
6767
* @param string $code code returned from facebook, exchange for access token
68-
* @return array $response
68+
* @return array
6969
*/
7070
function getAccessTokenWithCode( $code ) {
7171
// endpoint for getting an access token with code
@@ -80,4 +80,25 @@ function getAccessTokenWithCode( $code ) {
8080

8181
// make the api call
8282
return makeApiCall( $endpoint, 'GET', $params );
83+
}
84+
85+
/**
86+
* Get an access token with the code from facebook.
87+
*
88+
* Endpoint https://graph.facebook.com/{fb-graph-version}/{endpoint-path}
89+
*
90+
* @param array $params Params for fb endpoint.
91+
* @return array
92+
*/
93+
function getFacebookUserInfo( $params ) {
94+
// endpoint for getting an access token with code
95+
$endpoint = FB_GRAPH_DOMAIN . FB_GRAPH_VERSION . '/' . $params['endpoint_path'];
96+
97+
$endpointParams = array( // params for the endpoint
98+
'fields' => $params['fields'],
99+
'access_token' => $params['access_token']
100+
);
101+
102+
// make the api call
103+
return makeApiCall( $endpoint, $params['request_type'], $endpointParams );
83104
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
// include our defines file
3+
include 'defines.php';
4+
5+
// include php functions
6+
include 'functions.php';
7+
8+
$userInfoParams = array( // endpoint and params for getting a user
9+
'endpoint_path'=> 'me',
10+
'fields' => 'accounts,name',
11+
'access_token' => $userAccessToken,
12+
'request_type' => 'GET'
13+
);
14+
15+
// get user info from the api
16+
$userInfo = getFacebookUserInfo( $userInfoParams );
17+
18+
$pageInfoParams = array( // endpoint and params for getting page
19+
'endpoint_path'=> $pageId,
20+
'fields' => 'access_token',
21+
'access_token' => $userAccessToken,
22+
'request_type' => 'GET'
23+
);
24+
25+
// get page info from api
26+
$pageInfo = getFacebookUserInfo( $pageInfoParams );
27+
28+
// dump out results on page
29+
echo '<pre>';
30+
print_r($userInfo);
31+
print_r($pageInfo);
32+
die();

0 commit comments

Comments
 (0)