[Solved] Http post request on meteor



In meteor we can easily do a post request as explained in the doc; I try to create a post request using the code bellow, I didn’t get it to work; if you can help me with example, it ll be great

POST /oauth2/v4/token HTTP/1.1
Host: www.googleapis.com
Content-Type: application/x-www-form-urlencoded



@sashko, any help, insights here please ?


But where is Meteor code?! Give us some code example.



I send from the client the code to the server to get the refresh token like this:

  let code = window.location.href.split('code=')[1]; 
      let tokenMethod= Meteor.call('refreshToken',code, function(error,result){
        if (!error) {

and this the code from the server, to get the refresh token:

var google = require('googleapis');
var OAuth2 = google.auth.OAuth2;

var oauth2Client = new OAuth2(

  oauth2Client.getToken(code, function (err, tokens) {
	    // Now tokens contains an access_token and an optional refresh_token. Save them. 
	    //return code;
	    if (!err) {
	      let token = oauth2Client.setCredentials(tokens);
	      return tokens;
          return err;

     // return code;

    // let request = {
    //           headers: {
    //             'content-type': 'application/x-www-form-urlencoded',
    //           },
    //           params : {
    //             code: code,
    //             client_id: '***.apps.googleusercontent.com',
    //             client_secret: ********,
    //             redirect_uri: 'http://localhost:3000/EspacePerso',
    //             grant_type: 'authorization_code',
    //           }
    //         };
    //         let response = HTTP.post('https://www.googleapis.com/oauth2/v4/token', request,
    //              function (error, result) {
    //              if (!error) {
    //                // Session.set("twizzled", true);
    //                 console.log('resultat',result);
    //                 // return result;
    //              }
    //             });
    //         return 'response in server is: '+ response;


but in all cases, I get undefined.

Hop this code can help



Do you sure, what you have to use ‘params’? Try to use ‘data’


even with data I get undefined :frowning:


Maybe you have error? (-:
add else for error in callback…


the same thing, undefined; but if I return code in callback or after, I get the code value in client


Well… I created test app, add your code with fake data and get error in callback:

Error: failed [401] {  "error": "invalid_client",  "error_description": "The OAuth client was not found." } 
at makeErrorByStatus (http.js?hash=c5adc9c…:50)
at XMLHttpRequest.xhr.onreadystatechange (http.js?hash=c5adc9c…:292)

I have no idea, why you have undefined…


@none, the datas above are not the right one, they are just example form the doc, so probably the client id does not existe or the code is not correct in your case


But my code made call and got error… not undefined


the problem was my redirect_uri was not correct; thank’s