Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda