TL;DR: Access Laravel Passport API’s from Shell Script using CURL and JQ. This combination can be helpful in running remote Automation Jobs and Builds.

Laravel Passport is a full OAuth2 server implementation for Laravel. It provides a simple and convenient way to authenticate users and secure API endpoints. In this article, we’ll walk through the process of logging in to a Laravel Passport API using a shell script. We’ll use curl to make HTTP requests and jq to parse JSON responses.

Prerequisites

  1. Laravel Passport installed on your Laravel project.
  2. A valid Client ID and ** Client Secret** generated from Laravel Passport.
  3. Basic knowledge of ** shell scripting** , ** curl** , and ** jq**.

** For Passport Setup & Complete Code refer https://github.com/gdbhosale/laravel-passport-api-shell-curl-access**.

Set up the Shell Script

Open your favorite text editor and create a new file named login_script.sh. This will be our shell script file.

Next, define the variables that we’ll be using. These include the base URL of your Laravel project, the client ID, and the client secret.

#!/bin/bash

base_url="http://your-laravel-app.com"
client_id="your-client-id"
client_secret="your-client-secret"

Get Access Token

We’ll now use curl to request an access token from the Laravel Passport API. This token will be used to authenticate subsequent API requests.

access_token=$(curl -s -X POST -H "Accept: application/json" \
  -d "grant_type=password" \
  -d "client_id=$client_id" \
  -d "client_secret=$client_secret" \
  -d "username=your-username" \
  -d "password=your-password" \
  "$base_url/oauth/token" | jq -r '.access_token')

In this command:

  • -s to get output silently
  • -X POST specifies that we’re making a POST request.
  • -H "Accept: application/json" sets the header to accept JSON responses.
  • -d flags are used to send data in the request body.
  • jq -r '.access_token' extracts the access token from the JSON response.

Make API Requests

Now that we have the access token, we can use it to make authenticated API requests. For example, let’s say we want to retrieve user information:

user_info=$(curl -s -X GET -H "Accept: application/json" \
  -H "Authorization: Bearer $access_token" \
  "$base_url/api/user")

Here, we’re using the access token obtained in the previous step as part of the request header using -H "Authorization: Bearer $access_token".

Step 6: Parse JSON Response

Finally, use jq to parse the JSON response and extract the information you need.

username=$(echo "$user_info" | jq -r '.name')
email=$(echo "$user_info" | jq -r '.email')

echo "Username: $username"
echo "Email: $email"

In this example, we’re extracting the name and email fields from the JSON response.

Final script:

#!/bin/bash

base_url="http://laravel-passport-api-shell-curl-access.test"
client_id="3"
client_secret="AU43oYLwMrBVMuaxxhG636yMqsJytSaYVrIcikjU"

access_token=$(curl -s -X POST -H "Accept: application/json" \
  -d "grant_type=password" \
  -d "client_id=$client_id" \
  -d "client_secret=$client_secret" \
  -d "username=john@example.com" \
  -d "password=secret" \
  "$base_url/oauth/token" | jq -r '.access_token')

user_info=$(curl -s -X GET -H "Accept: application/json" \
  -H "Authorization: Bearer $access_token" \
  "$base_url/api/user")

username=$(echo "$user_info" | jq -r '.name')
email=$(echo "$user_info" | jq -r '.email')

echo "Username: $username"
echo "Email: $email"

Step 7: Execute the Script

Make the script executable by running:

chmod +x login_script.sh

Then, you can execute it:

./login_script.sh

Output:

Username: John Doe
Email: john@example.com

Find complete code on :https://github.com/gdbhosale/laravel-passport-api-shell-curl-access

Conclusion

In this article, we’ve demonstrated how to log in to a Laravel Passport API using a shell script. We used curl to make HTTP requests and jq to parse JSON responses. This script can serve as a foundation for automating API interactions in your Laravel project. Remember to handle sensitive information, such as client IDs and secrets, with care, and consider using environment variables or other secure methods for storage.