Solved

401 Unauthorized error while creating custom app

testAppKiss
Shopify Partner
5 1 2

Hi, 

I am trying to build a custom app, and I have given basic permission for the app .

For testing I wanted to know if I can access the product api or not.
For that I have some php script and trying to access the api this way.

$url = "https://$api_key:$password@$shop_url/admin/api/2023-10/customers.json";
 
While I run the code I get 401 error. 
Warning: file_get_contents(https://...@mywoodbuildkit.myshopify.com/admin/api/2023-10/products.json
Failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized.

Can someone help me why this issue occurs?
I am following the same way as I did for other apps but I am not able to find the solution 
Accepted Solutions (2)
testAppKiss
Shopify Partner
5 1 2

This is an accepted solution.

Hi, 
Thanks for the message. 
I tried to replace the code and I get 301 error now 

testAppKiss_0-1703611581709.png

 

View solution in original post

Samia_Akram22
Shopify Partner
60 10 14

This is an accepted solution.

A 301 error is a status code indicating that the requested resource has been permanently moved to a different URL. In the context of an API request, it might mean that the URL you're using has been redirected to another location.

There could be several reasons for a 301 error when making an API request:

1. Incorrect URL:
Ensure that the URL you're using is correct and properly formatted. It should point to the correct API endpoint. Check for any typos or mistakes in the URL.

2. API Version or Endpoint Change:
Shopify's API might have changed the endpoint or version. Verify the API version (`2023-10`) and the specific endpoint (`/admin/api/2023-10/products.json`) you're trying to access.

3. HTTP to HTTPS Redirection:
Shopify enforces HTTPS for API requests. If you're using an HTTP URL and Shopify redirects HTTP requests to HTTPS, you might encounter a 301 error. Make sure you're using `https://` in your API request URL.

4. Authentication Issues:
If the authentication credentials (`API key` and `password`) are incorrect, Shopify might redirect you to an error page or another location, resulting in a 301 error.

To troubleshoot the issue further, ensure that your URL is correctly formed, uses HTTPS, and points to the right API version and endpoint. Additionally, double-check your authentication credentials to confirm they're accurate.

Here's an example of using cURL in PHP with HTTPS:

```php
<?php
$api_key = 'YOUR_API_KEY';
$password = 'YOUR_PASSWORD';
$shop_url = 'YOUR_SHOP_URL';

$url = "https://$shop_url/admin/api/2023-10/products.json";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");

$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_status == 200) {
// Successful API call
echo "API call successful.";
// Handle $response data
} else {
// Error handling
echo "Error: $http_status - " . curl_error($ch);
}

curl_close($ch);
?>
```

Replace `'YOUR_API_KEY'`, `'YOUR_PASSWORD'`, and `'YOUR_SHOP_URL'` with your actual Shopify app credentials and shop URL. Also, ensure that the URL follows the correct structure and includes `https://`.

@testAppKiss  hope that helps.Thanks

Problem Solved? ✔Accept and Like solutions to help future merchants.

View solution in original post

Replies 4 (4)

Samia_Akram22
Shopify Partner
60 10 14

@testAppKiss Certainly! Here's a customized explanation for troubleshooting the 401 Unauthorized error while accessing the Shopify API in PHP:

1. Check Credentials:
First and foremost, ensure that the API key and password being used in your PHP script are accurate. Even minor typos or mistakes in these credentials can cause authentication failures.

2. Verify App Permissions:
Confirm that your app has the necessary permissions granted to access the Product API. Visit your Shopify Partner Dashboard or Admin section to check the app's API permissions. Ensure that it has the required read/write access to products.

3. Validate API Version and Endpoint:
Verify that the API version (in your case, `2023-10`) is the correct and supported version. Additionally, double-check the endpoint (`/admin/api/2023-10/products.json`) you're trying to access for retrieving product information.

4. Review API URL Structure:
Although your URL format seems correct, you might want to consider using alternative methods to handle HTTP requests, like utilizing cURL or an HTTP library that offers better error handling capabilities, particularly related to authentication issues.

5. Consider Rate Limits and Call Restrictions:
Check if your app is exceeding the API rate limits or facing any restrictions on the number of allowed API calls. Excessive API requests could trigger 401 errors until the limit resets.

6. Verify Shopify Service Status:
Occasionally, Shopify might experience service disruptions or issues with their API. Visit the Shopify Status page to determine if there are any ongoing problems affecting API access.

Here's an example of using cURL in PHP to handle the API request and potentially provide more detailed error information:

```php
<?php
$api_key = 'YOUR_API_KEY';
$password = 'YOUR_PASSWORD';
$shop_url = 'YOUR_SHOP_URL';

$url = "https://$shop_url/admin/api/2023-10/products.json";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");

$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_status == 200) {
echo "API call successful.";
// Handle $response data
} else {
echo "Error: $http_status - " . curl_error($ch);
}

curl_close($ch);
?>
```

Replace `'YOUR_API_KEY'`, `'YOUR_PASSWORD'`, and `'YOUR_SHOP_URL'` with your actual Shopify app credentials and shop URL.

If the issue persists after these steps, it might be beneficial to review the Shopify API documentation or contact Shopify support for further assistance, especially if there are specific settings affecting your app's access.

 

Problem Solved? ✔Accept and Like solutions to help future merchants.
testAppKiss
Shopify Partner
5 1 2

This is an accepted solution.

Hi, 
Thanks for the message. 
I tried to replace the code and I get 301 error now 

testAppKiss_0-1703611581709.png

 

Samia_Akram22
Shopify Partner
60 10 14

This is an accepted solution.

A 301 error is a status code indicating that the requested resource has been permanently moved to a different URL. In the context of an API request, it might mean that the URL you're using has been redirected to another location.

There could be several reasons for a 301 error when making an API request:

1. Incorrect URL:
Ensure that the URL you're using is correct and properly formatted. It should point to the correct API endpoint. Check for any typos or mistakes in the URL.

2. API Version or Endpoint Change:
Shopify's API might have changed the endpoint or version. Verify the API version (`2023-10`) and the specific endpoint (`/admin/api/2023-10/products.json`) you're trying to access.

3. HTTP to HTTPS Redirection:
Shopify enforces HTTPS for API requests. If you're using an HTTP URL and Shopify redirects HTTP requests to HTTPS, you might encounter a 301 error. Make sure you're using `https://` in your API request URL.

4. Authentication Issues:
If the authentication credentials (`API key` and `password`) are incorrect, Shopify might redirect you to an error page or another location, resulting in a 301 error.

To troubleshoot the issue further, ensure that your URL is correctly formed, uses HTTPS, and points to the right API version and endpoint. Additionally, double-check your authentication credentials to confirm they're accurate.

Here's an example of using cURL in PHP with HTTPS:

```php
<?php
$api_key = 'YOUR_API_KEY';
$password = 'YOUR_PASSWORD';
$shop_url = 'YOUR_SHOP_URL';

$url = "https://$shop_url/admin/api/2023-10/products.json";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERPWD, "$api_key:$password");

$response = curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($http_status == 200) {
// Successful API call
echo "API call successful.";
// Handle $response data
} else {
// Error handling
echo "Error: $http_status - " . curl_error($ch);
}

curl_close($ch);
?>
```

Replace `'YOUR_API_KEY'`, `'YOUR_PASSWORD'`, and `'YOUR_SHOP_URL'` with your actual Shopify app credentials and shop URL. Also, ensure that the URL follows the correct structure and includes `https://`.

@testAppKiss  hope that helps.Thanks

Problem Solved? ✔Accept and Like solutions to help future merchants.
testAppKiss
Shopify Partner
5 1 2

This issue is fixed.
There was error in the url