Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
970 B
2
Indexable
Never
 code = req.query.code;
  const formData = new URLSearchParams();
formData.append('grant_type', 'authorization_code');
formData.append('client_id', UID);
formData.append('client_secret', secret);
formData.append('code', code);
formData.append('redirect_uri', 'http://localhost:3000/afterAuth');

const options = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded',
    'Content-Length': formData.toString().length
  }
};

const request = https.request(tokenLink, options, (response) => {
  let responseData = '';

  response.on('data', (chunk) => {
    responseData += chunk;
  });

  response.on('end', async () => {
    await client.connect();
    token =  JSON.parse(responseData)['access_token'];
    if (token != null)
        await oauthColl.updateOne({replace: "me"}, {$set : {token: token}});
  });
});
request.on('error', (error) => {
  console.error('Error:', error);
});

request.write(formData.toString());
request.end();