lelylan / simple-oauth2

A simple Node.js client library for Oauth2
http://lelylan.github.com/simple-oauth2/
1.62k stars 292 forks source link

Access Token error URL.url is not a contructor #423

Closed Ugesh2204 closed 1 year ago

Ugesh2204 commented 1 year ago

I am get an error while retrieving access token. Access Token error URL.url is not a contructor.

My credential are working find with Postman. Don't know why am getting this error. Any help or explanation would be a great thanks. My below code implementation

const getToken = async () => {
  if (spAccessToken && !spAccessToken.expired(300)) {
    return spAccessToken;
  }
  const config = {
    client: {
      id: process.env.NEXT_PUBLIC_CLIENT_ID,
      secret: process.env.NEXT_PUBLIC_CLIENT_SECRET,
    },
    auth: {
      tokenHost: process.env.NEXT_PUBLIC_ACCESS_TOKEN_URL,
      tokenPath: process.env.NEXT_PUBLIC_TENANT_ID + "/oauth2/token",
    },
    options: {
      bodyFormat: "form",
      authorizationMethod: "body",
    },
  };
  const tokenConfig = {
    scope: "",
    resource: process.env.NEXT_PUBLIC_CLIENT_ID,
  };
  const { ClientCredentials } = require("simple-oauth2");
  const client = new ClientCredentials(config);

  try {
    console.log(config);
    const accessToken = await client.getToken(tokenConfig);
    console.log("get token");
    spAccessToken = accessToken.token;
    return accessToken;
  } catch (error) {
    // console.log(error.data);
    console.log(error.message);
    return;
  }
};
jonathansamines commented 1 year ago

I'll close this issue as it lacks the required template information. Feel free to re-open with missing details.