0

I am trying to create a Plugin that does the following jobs:

  1. Authenticate the user to get Bearer token 'access_token'
  2. Get data from API and store in Database

I have checked all the APIs and endpoints using Postman and they successfully received the desired responses. I created a folder 'MYAPI' inside the 'Plugins' directory as per the WordPress plugin development documentation and also created the necessary files.

To authenticate the API, from what I understand I need to use the wp_safe_remote_request function and I did. The following is my entire code for the authentication of the user to get a bearer token for the further API requests.

function authenticate_api() {
    $user_id = '[email protected]';
    $password = '123456';

    $auth_url = 'https://my-example-url.com/oauth/token';

    $headers = array(
        'Content-Type' => 'application/x-www-form-urlencoded',
        'Authorization' => 'Basic Base64enodedusercredentials',
    );

    $post_data = array(
        'grant_type' => 'password',
        'username' => $user_id,
        'password' => $password,
    );

    $response = wp_safe_remote_request($auth_url, array(
        'method' => 'POST',
        'headers' => $headers,
        'body' => $post_data,
    ));

    if (is_wp_error($response)) {
        return 'API request error: ' . $response->get_error_message();
    }

    $status_code = wp_remote_retrieve_response_code($response);

    if ($status_code === 200) {
        $response_body = wp_remote_retrieve_body($response);
        $auth_data = json_decode($response_body, true);

        if (isset($auth_data['access_token'])) {
            return 'API authentication succeeded. Bearer token: ' . esc_html($auth_data['access_token']);
        }
    } else {
        return 'API authentication failed. HTTP status code: ' . $status_code;
    }
}

I also wrote a piece of code that generates a shortcode to check the status of the response.

function api_authentication_test_shortcode() {
    // Attempt API authentication.
    $token = authenticate_api();

    if ($token) {
        return 'API authentication succeeded. Bearer token: ' . esc_html($token);
    } else {
        return 'API authentication failed. Please check your credentials.';
    }
}

// Register the shortcode.
add_shortcode('api_authentication_test', 'api_authentication_test_shortcode');

Unfortunately when I use the shortcode [api_authentication_test] on a test page, I am shown the following

API authentication succeeded. Bearer token: API authentication failed. HTTP status code: 401

I have tried removing the $post_data and put them in the URL as it is when making the request using Postman. I have also tried using explode() to get just the bearer token and still the same output. I appreciate any help.

1 Answer 1

0

If the API requires an authorization as Bearer token, just write is as Bearer and not as Basic:

$headers = array(
        'Content-Type' => 'application/x-www-form-urlencoded',
        'Authorization' => 'Beaerer Base64enodedusercredentials',
    );

Also take a look at the documentation of this API.

Also, make sure that you are currently duplicating the feedback in both of your functions. The 2nd function would be completely sufficient:

function api_authentication_test_shortcode() {
    return authenticate_api();
}
add_shortcode('api_authentication_test', 'api_authentication_test_shortcode');
1
  • 1
    In order to get the Bearer token I need to Authenticate the 'User" first. Upon Authenticating the user, the response I get will have the access_token aka Bearer token. But i scrapped the entire plugin, and started from the beginning and I am getting the bearer token now. Although I'm still not sure what went wrong with the code above. Also, thank you for the help.
    – I. Antonov
    Oct 8 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.