Create get_auth_token.js

This commit is contained in:
5vl 2023-04-06 21:25:16 +02:00 committed by GitHub
parent c6e73f2cab
commit 552d1bbd07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

37
get_auth_token.js Normal file
View File

@ -0,0 +1,37 @@
// To use any npm package, just import it
import axios from "axios"
import qs from "qs"
export default defineComponent({
props: {
store: {
type: "data_store",
},
},
async run({ steps, $ }) {
const currTime = Date.now()
const oldTime = await await this.store.get("time")
if (currTime < oldTime) {
const token = await this.store.get("access_token")
return token
}
const refresh_token = await this.store.get("refresh_token")
let data = await axios.post("https://api.twitter.com/2/oauth2/token", qs.stringify({
"refresh_token": refresh_token,
"grant_type": "refresh_token",
"client_id": "CLIENT_ID"
}),
{
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Basic BASIC_TOKEN"
}
}
)
data = data.data
await this.store.set("refresh_token", data.refresh_token)
await this.store.set("access_token", data.access_token)
await this.store.set("time", currTime + 3600000)
return data.access_token
},
})