"errorCode": 1002, "message": "Your Access Token is invalid."

GNCOM
GNCOM ✭✭
edited 01/30/24 in API & Developers

Hi

I have problem with API key. I have tried PHP and Python code's but every time I get same error. I want to get sheet row ID's.

This is my PHP code:

<?php

$apiKey = "API_KEY"; // in my code I replace it with real API key

$sheetID = 4969625754404740;

$apiUrl = "https://api.smartsheet.com/2.0/sheets/$sheetID";


$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $apiUrl);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(

  "Authorization: Bearer $apiKey",

  "Content-Type: application/json"

));

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


$response = curl_exec($ch);

curl_close($ch);


$response = curl_exec($ch);

if (curl_errno($ch)) {

  echo "CURL Error: " . curl_error($ch);

}

echo $response;


if ($response) {


  $data = json_decode($response, true);

   

  if (isset($data["columns"])) {

    echo "Columns in the sheet:\n";

    foreach ($data["columns"] as $column) {

      echo "Name: " . $column["title"] . ", ID: " . $column["id"] . "\n";

    }

  } else {

    echo "No columns found in the sheet.";

  }

} else {

  echo "Failed to retrieve data from Smartsheet.";

}

?>

And this is error what I get:

{ "errorCode" : 1002, "message" : "Your Access Token is invalid.", "refId" : "sphoti" }No columns found in the sheet.

I have generated 5-6 different API keys (for now I am deleted some) and every time same result. What I am doing wrong?


Answers

  • jmyzk_cloudsmart_jp
    jmyzk_cloudsmart_jp ✭✭✭✭✭✭

    Hi @GNCOM

    I would check those first;

    • Ensure your API key has the necessary permissions to access sheet data. 
    • Sheet ID: Double-check that the sheet ID is correct
    • A redundant call to curl_exec($ch); after curl_close($ch)

    Here is a Python equivalent of your code using Smartsheet Python SDK.

    https://smartsheet.redoc.ly/tag/sheets#operation/getSheet

    You'll need to install the Smartsheet SDK for Python first. If you haven't already installed it, you can do so using pip:

    pip install smartsheet-python-sdk
    

    Make sure to replace 'YOUR_API_KEY' with your actual Smartsheet API key and the sheet ID with the actual ID of the sheet you want to access.

    import smartsheet
    
    # Replace 'YOUR_API_KEY' with your actual Smartsheet API key
    api_key = #'YOUR_API_KEY'
    sheet_id = 4892716795488132  # Replace with your actual sheet ID
    
    # Initialize client
    smartsheet_client = smartsheet.Smartsheet(api_key)
    
    try:
        # Get sheet
        sheet = smartsheet_client.Sheets.get_sheet(sheet_id)
    
        print("Columns in the sheet:")
        for column in sheet.columns:
            print(f"Name: {column.title}, ID: {column.id}")
    
    except smartsheet.exceptions.ApiError as e:
        print(f"Error: {e}")
    
    except Exception as e:
        print(f"An error occurred: {e}")
    
  • GNCOM
    GNCOM ✭✭

    Thank you for your answers but this still wont work.

    Something is wrong with my api keys and I dont know what. How I check that my API key has the necessary permissions to access sheet data? I am all folders and sheets owner, is this isn't enough?

  • jmyzk_cloudsmart_jp
    jmyzk_cloudsmart_jp ✭✭✭✭✭✭

    @GNCOM

    Your PHP code may need some help. Have you tried my Python code? It is working for my token.

    Smartsheet API has C#, Java, Node.js, Python, and Ruby SDK. Unfortunately, they don't offer PHP SDK. Developing with SDK is efficient, so if you can use Python, I suggest you try it.

  • GNCOM
    GNCOM ✭✭

    I tried Python also and your code but getting same error. I installed smartsheet-python-sdk to my server and installation was successful but I dont know what I am doing wrong. When I generate API key I do copy paste for it and still it says that API key is invalid. Don't know what to try because basic elementary code wont work.

  • GNCOM
    GNCOM ✭✭

    I found the error. Everywhere in the examples, the smartsheet.com URL is used, but as I am a European user, I need to use the smartsheet.eu address. The error was found and now everything works.

  • jmyzk_cloudsmart_jp
    jmyzk_cloudsmart_jp ✭✭✭✭✭✭

    Glad to hear you fixed the issue with the smartsheet.eu address!

  • SpeedSheet99
    SpeedSheet99 ✭✭
    edited 01/30/24

    Had the same issue, here is how i fixed it :

    In the __init__.py file of the SDK the urls are defined

    __api_base__ = "https://api.smartsheet.com/2.0"

    __eu_base__ = "https://api.smartsheet.eu/2.0"

    __gov_base__ = "https://api.smartsheetgov.com/2.0"

    __api_version__ = "2.0"

    so in the smartsheet.py i added the eu_base import :

    from . import __api_base__, __version__, models, __eu_base__

    then in the smartsheet class i changed the api_base :

           #api_base=__api_base__,

           api_base=__eu_base__,

    And it worked fine