Fetch error on Office Script (Does Bearer Authorization token for external calls work in Excel script?)

Jose Luis Risueño 0 Reputation points
2023-03-21T10:13:41.62+00:00

Hi all, I am trying to run the following script for an external API call with a Bearer Token:

export interface DATOS {
    count: number;
    results: Result[];
    skill_memberships: SkillMemberships;
    meta: Meta;
}
export interface Meta {
    count: number;
    page_count: number;
    page_number: number;
    page_size: number;
}
export interface Result {
    key: string;
    id: string;
}
export interface SkillMemberships {
    "87530265": The87530265;
}
export interface The87530265 {
    level: number;
    max_level: number;
    created_at: string;
    updated_at: string;
    cached_skill_name: string;
    skill_id: string;
    creator_id: string;
    user_id: string;
    id: string;
}
export interface TESTBASIC {
    userId: number;
    id: number;
    title: string;
    body: string;
}
async function main(workbook: ExcelScript.Workbook): Promise<void>
{
    var myHeaders = new Headers();
    myHeaders.append("Authorization", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
    console.log("Test2");
    let url = encodeURI('https://api.xxxxxxxxxxxx.com/');
    try
    {
        let fetchResult = await fetch(url, {
            method: 'GET',
            headers: {                
                "Access-Token": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                "Content-Type": "application/json",
                "Accept": "application/json"
            },
            redirect: 'follow'
        });
        let json: DATOS = await fetchResult.json();
        console.log(json);
    } catch(e)
    {
        console.log(e);
    }
}

But every time I try to run it to show me the result in the console, I always get the following error:

{message: "Failed to fetch", name: "TypeError", stack: "TypeError: Failed to fetch at 
self.fetch (https://c0a4c0b1-8f01-4eaa-84f0-6434f1eb8473.officescripts.microsoftusercontent.com/eed0a086-3498-4f91-a377-1e0265ecc0cc/worker.js:99:13) 
at main (blob:https://c0a4c0b1-8f01-4eaa-84f0-6434f1eb8473.officescripts.microsoftusercontent.com/5db7939c-4ae9-4802-8c9f-aed101943d56:183:45) 
at async _____wrapperMain (blob:https://c0a4c0b1-8f01-4eaa-84f0-6434f1eb8473.officescripts.microsoftusercontent.com/5db7939c-4ae9-4802-8c9f-aed101943d56:209:20)"}

One of the doubts that I have regarding this script is if Excel Office Script is directly capable of working with this type of tokens, because perhaps if it cannot work with this type of tokens, this type of error will always appear, is this true? ? Or maybe this is due to an error in the text structure or interfaces? Is there a way to do this via Office Script, or would it only be possible to do it via VBA?

Regards

JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
982 questions
Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,896 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yutao Huang - MSFT 701 Reputation points Microsoft Employee
    2023-03-21T15:35:06.05+00:00

    Fetch calls to external APIs (no matter requiring bearer auth headers or not) should work in Office Scripts as long as:

    For troubleshooting, you can also open the browser's developer tools panel (press F12) and keep an eye on the Network tab and see if there are any errors from that fetch API request while running your script. Sometimes you can also take a look at the Console tab to see if there is anything unexpected.

    Also I noticed you declared a myHeaders variable with a header named Authorization. But in the actual fetch call, you used another header named Access-Token. Is that intentional?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.