AleksandrRogov / DynamicsWebApi

DynamicsWebApi is a Microsoft Dataverse Web API helper library for JavaScript & TypeScript
MIT License
272 stars 59 forks source link

fetch function does dot respect the token property. #175

Closed saostad closed 3 months ago

saostad commented 3 months ago

DynamicsWebApi version "dynamics-web-api": "^2.1.4",

Describe the bug When you provide a token directly in the configuration (using the token property), it works for the retrieve function but not for fetch or fetchAll functions. These latter functions seem to only work when you provide the onTokenRefresh function.

Expected behavior get the result from api.

Actual result '401_error_reason': 'Authorization header is missing.',

Code Snippet

const dynamicsWebApi = new DynamicsWebApi({
    serverUrl: input.serverUrl,
  });

const xmlQuery = ``;   // something that we sure works.

const response = await dynamicsWebApi.fetchAll({
      collection: "opportunities",
      token: input.token,  // string token value
      fetchXml: xmlQuery
    });

Your Setup (please complete the following information):

Additional Info: this is a workaround for the bug:

const dynamicsWebApi = new DynamicsWebApi({
    serverUrl: input.serverUrl,
    onTokenRefresh: async () => {
      return input.token;
    }
  });

const xmlQuery = ``;   // something that we sure works.

const response = await dynamicsWebApi.fetchAll({
      collection: "opportunities",
      fetchXml: xmlQuery
    });
AleksandrRogov commented 3 months ago

hi @saostad , thank you for submitting the issue. Going to investigate that.

AleksandrRogov commented 3 months ago

@saostad can't reproduce it. I thought I have, but there was a mistype in my tests.

saostad commented 3 months ago

@saostad can't reproduce it. I thought I have, but there was a mistype in my tests.

  • Are you sure that your token is not empty?
  • How big is your fetch xml? Does it contain more than ~2000 characters?

the token is not empty I am sure about it. the fetch XML is 2512 characters.

AleksandrRogov commented 3 months ago

@saostad ok, this is the only case I haven't tested. Is when fetch is converted into a batch operation. Going to continue investigation. Thanks.

AleksandrRogov commented 3 months ago

@saostad as another workaround you can set headers manually:

const response = await dynamicsWebApi.fetchAll({
  collection: "opportunities",
  fetchXml: xmlQuery,
  headers: {
    Authorization: `Bearer ${input.token}`
  }
});
AleksandrRogov commented 3 months ago

@saostad should be fixed in 2.1.5. If you continue having issues, let me know.