export type OAuth2TokenResponse = {
access_token: string;
token_type: string;
expires_in: number;
refresh_token: string;
scope: string;
created_at: number;
};
export default {
async fetch(request, env, ctx): Promise<Response> {
const endpoint = `${env.ETP_ROOT}/oauth/token`;
const headers = new Headers();
headers.append("Content-Type", "application/json");
const raw = JSON.stringify({
"grant_type": "password",
"username": env.ETP_USERNAME,
"password": env.ETP_PASSWORD,
"client_id": env.ETP_CLIENT_ID,
"client_secret": env.ETP_CLIENT_SECRET
});
const requestOptions = {
method: "POST",
headers: headers,
body: raw,
redirect: "follow"
} as RequestInit;
const response = await fetch(endpoint, requestOptions)
console.log(response.status);
const tokenResponse = await response.json() as unknown as OAuth2TokenResponse;
console.log(JSON.stringify(tokenResponse));
console.log(`Response token issued: expires in: ${tokenResponse.expires_in}`);
return new Response(`Response token issued: expires in: ${tokenResponse.expires_in}`);
},
} satisfies ExportedHandler<Env>;
export type OAuth2TokenResponse = {
access_token: string;
token_type: string;
expires_in: number;
refresh_token: string;
scope: string;
created_at: number;
};
export default {
async fetch(request, env, ctx): Promise<Response> {
const endpoint = `${env.ETP_ROOT}/oauth/token`;
const headers = new Headers();
headers.append("Content-Type", "application/json");
const raw = JSON.stringify({
"grant_type": "password",
"username": env.ETP_USERNAME,
"password": env.ETP_PASSWORD,
"client_id": env.ETP_CLIENT_ID,
"client_secret": env.ETP_CLIENT_SECRET
});
const requestOptions = {
method: "POST",
headers: headers,
body: raw,
redirect: "follow"
} as RequestInit;
const response = await fetch(endpoint, requestOptions)
console.log(response.status);
const tokenResponse = await response.json() as unknown as OAuth2TokenResponse;
console.log(JSON.stringify(tokenResponse));
console.log(`Response token issued: expires in: ${tokenResponse.expires_in}`);
return new Response(`Response token issued: expires in: ${tokenResponse.expires_in}`);
},
} satisfies ExportedHandler<Env>;