how to make a POST request?

Asked

Viewed 394 times

0

The thing is, I’m using the google api to build an app. However, I got to this part below and I can’t do it at all. How do I do this POST q it asks me? I already have all the parameter data.

Below the google help snippet:

Supondo que o usuário tenha acesso a seu aplicativo, troque o código de autorização recebido na etapa 3 por um toque de atualização e um token de acesso. Envie uma solicitação POST para https://accounts.google.com/o/oauth2/token

Veja um exemplo de solicitação abaixo:

POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

    code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&
    client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&
    client_secret=hDBmMRhz7eJRsM9Z2q1oFBSe&
    redirect_uri=http://localhost/oauth2callback&
    grant_type=authorization_code

And this post was to return me the following:

{
  "access_token" : "ya29.AHES6ZTtm7SuokEB-RGtbBty9IIlNiP9-eNMMQKtXdMP3sfjL1Fc",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : "1/HKSmLFXzqP0leUihZp2xUt3-5wkU7Gmu2Os_eBnzw74"
}

1 answer

1


Assuming the data you’re going to send is in a variable called data:

var data = {
  code : "4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&",
  client_id :" 1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&",
  client_secret" : "hDBmMRhz7eJRsM9Z2q1oFBSe&",
  redirect_uri : "http://localhost/oauth2callback&",
  grant_type="authorization_code"
}

var request = new XMLHttpRequest();
request.open('POST', '/my/url', true);
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
request.send( data );

Reference: You Might not need jQuery

  • Blz, I’m going to do a test. How do I save the parameters in the date variable? Can I do date = 'parameters?'

  • I will edit the answer with these instructions.

  • See, man, you did. However, he gave this error: Xmlhttprequest cannot load https://accounts.google.com/o/oauth2/token. No 'Access-Control-Allow-Origin' header is present on the requested Resource. Origin 'http://localhost' is therefore not allowed access. The Response had HTTP status code 400. q may be?

  • This error means that the server that is receiving your post is not configured to receive requests from the localhost address. In this case the solution varies according to the API or service you want to send.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.