forked from sparkapi/sparkapi4p2
-
Notifications
You must be signed in to change notification settings - Fork 0
OAuth2 Only Authentication
wlmcewen edited this page Nov 7, 2012
·
9 revisions
One of the options to authenticate with is using 'OAuth2' alone method. This page goes over the OAuth2 Authentication:
// Sets the OAuth2 only Authentication
$api = new SparkAPI_OAuth($client_id, $client_secret, $application_uri);
// This is similar to our OpenId and OAuth2 Hybrid authentication call. The interface for each client is identical.
// To build the URI to redirect the end user to, invoke the "authentication_endpoint_uri" method, e.g.:
header("Location: " . $api->authentication_endpoint_uri());
// After this "code" is retrieved and you are redirected back, you will need to issue a Grant request with the "code" value brought back. Once the Grant is returned, you'll want to test that it was successful and set your Access Token and Refresh Token
$grant = $api->Grant($result, "authorization_code");
if ($grant == true) {
// These values can be saved and reused in future request then.
$accesst = $api->SetAccessToken($api->oauth_access_token);
$refresht = $api->SetRefreshToken($api->oauth_refresh_token);
} else {
print_r("Something went wrong.");
echo "API Error Code: {$api->last_error_code}<br>\n";
echo "API Error Message: {$api->last_error_mess}<br>\n";
};
// After this is settled, you should be able to make pulls from the system, for example:
$resultGetListings = $api->GetListings();
if ($resultGetListings === false) {
echo "API Error Code: {$api->last_error_code}<br>\n";
echo "API Error Message: {$api->last_error_mess}<br>\n";
exit;
} else {
print_r($resultGetListings);
}