import type { IActivityHandler } from "@vertigis/workflow"; interface Auth { appId: string; refreshToken: string; } /** An interface that defines the inputs of the activity. */ interface GetUserTokenInputs { /** * @displayName Portal Url * @description The second input to the activity. */ portalurl?: string; // Ensure this is the correct type } /** An interface that defines the outputs of the activity. */ interface GetUserTokenOutputs { /** * @description The result of the activity. */ access_token: string; } /** * @displayName Get User Token * @category EBTA Utilities * @description Get User Token 1.2 */ export default class GetUserTokenActivity implements IActivityHandler { /** Perform the execution logic of the activity. */ async execute(inputs: GetUserTokenInputs): Promise { // Your existing code here console.log("GetUserTokenActivity version: 1.2"); const authData = sessionStorage.getItem("esriJSAPIOAuth"); let auth:Auth = { appId: 'your_client_id', refreshToken: 'your_refresh_token' }; if (authData !== null) { const parsedAuthData = JSON.parse(authData); const portalAuth = parsedAuthData?.['/']?.[inputs.portalurl || 'https://www.arcgis.com']; if (typeof portalAuth === 'object') { auth = portalAuth; } } const url = "https://www.arcgis.com/sharing/rest/oauth2/token"; if (auth) { const myHeaders = new Headers(); myHeaders.append("Content-Type", "application/x-www-form-urlencoded"); const urlencoded = new URLSearchParams(); urlencoded.append("client_id", (auth.appId)); urlencoded.append("refresh_token", auth.refreshToken); urlencoded.append("grant_type", "refresh_token"); const requestOptions = { method: "POST", headers: myHeaders, body: urlencoded.toString() }; try { const response = await fetch(url, requestOptions); console.log(response) if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json() .then(responseData => { const data = responseData; return { access_token: data.access_token }; }) .catch(error => { console.error('Error processing response:', error); throw error; }); } catch (error) { console.error("Error fetching access token:", error); throw error; } } else { // Handle the case where auth is null console.error("Authentication data is missing."); // You can throw an error or handle it as per your application's requirement throw new Error("Authentication data is missing."); } } }