Skip to content

Instantly share code, notes, and snippets.

@vishy-experiments
Last active August 22, 2019 06:22
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 vishy-experiments/217441363b1271d3e1647585a0f9405b to your computer and use it in GitHub Desktop.
Save vishy-experiments/217441363b1271d3e1647585a0f9405b to your computer and use it in GitHub Desktop.
Laravel Passport Password Grant token request. Uses config's "app.url" to make it environment agnostic.
<? php
/*
|--------------------------------------------------------------------------------
| Environment Agnostinc: Requesting Password Grant Tokens Laravel using Passport
|--------------------------------------------------------------------------------
|
| Ref: https://laravel.com/docs/5.8/passport#requesting-password-grant-tokens
| Set the environment url in .env. Example: http://localhost for local dev server
| Use config('app.url') in the post request
| Makes makes it easy for testing APIs in tools like Postman
| Returns JSON
|
*/
$http = new GuzzleHttp\Client;
// Make sure "APP_URL" is properly set in the .env file
$oAuthUrl = config('app.url') . '/oauth/token';
try {
$response = $http->post($oAuthUrl, [
'form_params' => [
'grant_type' => 'password',
'client_id' => YOUR_PASSPORT_CLIENT_ID,
'client_secret' => 'YOUR PASSPORT CLIENT SECRET',
'username' => $request->username,
'password' => $request->password,
'scope' => '*',
],
]);
return response()->json(['token' => json_decode((string) $response->getBody(), true)], 200);
} catch (\Exception $e) {
return response()->json(['error'=>$e->getMessage()], $e->getCode());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment