Skip to content

Instantly share code, notes, and snippets.

@kushyapp
Last active May 29, 2018 22:44
Show Gist options
  • Save kushyapp/3fbdc484f557dec9743ca4970fb864dd to your computer and use it in GitHub Desktop.
Save kushyapp/3fbdc484f557dec9743ca4970fb864dd to your computer and use it in GitHub Desktop.
Kushy API OAuth Example - Routes
<?php
Route::get('/', function () {
$query = http_build_query([
'client_id' => '6',
'redirect_uri' => 'http://127.0.0.1:8001/callback',
'response_type' => 'code',
'scope' => 'access-email'
]);
return redirect('https://kushy.net/oauth/authorize?'.$query);
});
Route::get('/callback', function (Request $request) {
$response = (new GuzzleHttp\Client)->post('https://kushy.net/oauth/token', [
'form_params' => [
'grant_type' => 'authorization_code',
'client_id' => '6',
'client_secret' => 'q1w51ti3M0Mc79nTDkvQvpv2A8uvKDqpSJRMWlzK',
'redirect_uri' => 'http://127.0.0.1:8001/callback',
'code' => $request->code,
]
]);
session()->put('token', json_decode((string) $response->getBody(), true));
return redirect('/user');
});
Route::get('/user', function () {
if ( ! session()->has('token')) {
return redirect('/');
}
$response = (new GuzzleHttp\Client)->get('https://kushy.net/api/apps/v3/user', [
'headers' => [
'Authorization' => 'Bearer '.Session::get('token.access_token')
]
]);
return json_decode((string) $response->getBody(), true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment