Hi,
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
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2.example.com/code&
grant_type=authorization_code
@sashko , any help, insights here please ?
none
December 12, 2016, 8:36am
3
But where is Meteor code?! Give us some code example.
@none ,
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) {
console.log('result',result);
}else{
console.log('error',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(
YOUR_CLIENT_ID,
YOUR_CLIENT_SECRET,
YOUR_REDIRECT_URL
);
Meteor.methods({
'refreshToken'(code){
oauth2Client.getToken(code, function (err, tokens) {
// Now tokens contains an access_token and an optional refresh_token. Save them.
//return code;
if (!err) {
console.log('tokens',tokens);
let token = oauth2Client.setCredentials(tokens);
return tokens;
}else{
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
Thank’s
none
December 12, 2016, 9:08am
5
Do you sure, what you have to use ‘params’? Try to use ‘data’
@none
even with data I get undefined
none
December 12, 2016, 9:15am
7
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
none
December 12, 2016, 9:30am
9
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
none
December 12, 2016, 9:38am
11
But my code made call and got error… not undefined
the problem was my redirect_uri was not correct; thank’s