Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

199
Views
SharePoint cannot get Access Token with JavaScript?

I need to get access token from SharePoint, In order to upload some files! I got access token from postman successfully, But when I try to do the same request with Javascript!

const generateToken = async () => {
  const headers = { "Content-Type": "application/x-www-form-urlencoded" };
  var formdata = {};
  formdata["grant_type"] = "client_credentials";
  formdata["client_id"] = "<client_id>";
  formdata["client_secret"] = "<client_secret>";
  formdata["resource"] =
    "00000003-0000-0ff1-ce00-000000000000/site_url@tenant_id";
  const body = Object.keys(formdata)
    .map((key) => `${key}=${formdata[key]}`)
    .join("&");
  var requestOptions = {
    method: "POST",
    headers,
    body,
  };
  await fetch(
    "https://accounts.accesscontrol.windows.net/<tenant_id>/tokens/OAuth/2",
    requestOptions
  )
    .then((response) => response.json())
    .then((result) => console.log(result))
    .catch((error) => console.log("error", error));
};
generateToken();

when I execute the page which have this script I got this error

error TypeError: Failed to fetch
    at generateToken

But IDK why the respose status is 200 OK, without returning body which contain access_token

enter image description here

Any help is much appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You cannot get the token from javascript like that, only from single page applications because of the security issues: you expose your client secret.

You can use Microsoft Authentication Library (MSAL) for JS instead.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!