Last active
August 22, 2019 06:22
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? 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