Skip to content

Instantly share code, notes, and snippets.

@mtvbrianking
Last active September 15, 2018 20:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mtvbrianking/ed9f3bc3c509f42fc09d338865778443 to your computer and use it in GitHub Desktop.
Save mtvbrianking/ed9f3bc3c509f42fc09d338865778443 to your computer and use it in GitHub Desktop.
Laravel Passport OAuth2 consume own API via Javascript

PHP Curl

curl -X GET \
  http://localhost:8000/api/user \
  -H 'Accept: application/json' \
  -H 'Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6ImY3OTllMTZiODAzZjk4ZmU4MGQyZWYzYWFhM2IwODdmZGYwNjZjNmU1MzU2OWMzZjJjMmQ2ZDMzZGZkZmM1M2NjZmFiZTM4MmExMDZlZTczIn0.eyJhdWQiOiIxIiwianRpIjoiZjc5OWUxNmI4MDNmOThmZTgwZDJlZjNhYWEzYjA4N2ZkZjA2NmM2ZTUzNTY5YzNmMmMyZDZkMzNkZmRmYzUzY2NmYWJlMzgyYTEwNmVlNzMiLCJpYXQiOjE1MzcwMTY2MDcsIm5iZiI6MTUzNzAxNjYwNywiZXhwIjoxNTY4NTUyNjA2LCJzdWIiOiIxIiwic2NvcGVzIjpbXX0.Dy1WJQAHuOJgTEZ0SEJUd09_nO27IW7WpahhaFPDB7gOS5xqps3Cg3lWi663HuZ_8Ik4iXZ5zoLnoFc-0IQAEk86ZZ29EVOdhYN1LUBe4v8kyBc8JgJ5dMamY2dAtwCg_qL4ptxv0gyoZfV2Ot9lxEI9ZbP4vYHGef9o9RKorug8H3J9dm6pIHP6Bd33pO6AaLsxBNedDOAN20oAyhRRbAaeGoFwFyIp208M2Bpnc_Pku1xZUJjXZ-NVBNmpTB69llMti3qjBAHPPFo1br3-2NhGpGwzoYf1ClYrAiBxT58HZAtVIhQJB76fBtqarnObqoitVrTcxlLmUcNnnlTUUKoaupoFkpK-GzjZu-3-ZcJuijK8ZWl2FvhPm_JSRrmNZDtn3D7QMokBz8j-3VwlRB3r6ssJhGj-WzogwyBLTjP_08i2sAKxJStnAYk8SjH5DrUPyxRXiJqcfyUGs2qPZk28uZ38g2y7DsA9XQi-u8KlahRuCbhcGyHmOCQR28PG_gW2zPlMFjnGjaKdjT_rhUqj_aysrnkhotSn4ADmVn6O_W91fAhve4a2UbBfHyELbhQ8kpwMUh6ABJ0HBny4KKI8gknWLZb_WpyaaZH-uKQHlc2e4BzA5fnF9VZU3HpkDVViQIjz9Pw_it5Tg09cN8cpi4tmInyqizZSSK1X-YY' \
  -H 'Cache-Control: no-cache' \

VueJS -> Axios

axios.get('api/user').then(response => {
    console.log(response.data);
}).catch(error => {
    console.log(error.response.data);
});

jQuery -> Ajax

window.$.ajax({
    headers: {
        'Accept': 'application/json',
        'X-Requested-With': 'XMLHttpRequest',
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    type: 'GET',
    url: 'api/user',
    success: function (response) {
        console.log(response);
    },
    error: function (xhr) {
        console.error(xhr.responseText);
    }
});

Guzzle http client

CreateFreshApiToken only works for JS -> https://stackoverflow.com/q/52347569/2732184

Guzzle can't work with php artisan serve url, say http://localhost:8000

try {
    $client = new Client([
        'base_uri' => 'http://localhost/oauth-server/public/api/',
        'headers' => [
            'Accept' => 'application/json',
        ],
    ]);

    $api_response = $client->request('GET', 'user', ['debug' => true]);

    $user = json_decode((string) $api_response->getBody(), true);

    return response()->json($user);
} catch (ConnectException $ex) {
    return response()->json(['code' => $ex->getCode(), 'message' => $ex->getMessage()]);
} catch (ClientException $ex) {
    return json_decode($ex->getResponse()->getBody(), true);
} catch (ServerException $ex) {
    return response()->json(['code' => $ex->getCode(), 'message' => $ex->getMessage()]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment