Skip to content

Instantly share code, notes, and snippets.

@sj82516
Last active June 4, 2018 07:45
Show Gist options
  • Select an option

  • Save sj82516/70f15cdb6d085f631f0e77caefa76aa4 to your computer and use it in GitHub Desktop.

Select an option

Save sj82516/70f15cdb6d085f631f0e77caefa76aa4 to your computer and use it in GitHub Desktop.
const jwt = require('jsonwebtoken');
// 剛才下載的檔案
const googleServerKey = require("./test-f9099.json")
const token = jwt.sign({
"iss": googleServerKey.client_email,
scope,
"aud": "https://www.googleapis.com/oauth2/v4/token",
"exp": Math.floor(Date.now() / 1000) + (60 * 60),
"iat": Math.floor(Date.now() / 1000)
}, googleServerKey.private_key, {
header: {
"alg": "RS256",
"typ": "JWT"
}
})
function getAccessTokenByJWT() {
let data = qs.stringify({
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
assertion: token,
});
const headers = {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
};
return axios.post('https://www.googleapis.com/oauth2/v4/token', data, {
headers
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment