Jump to content

I am unable to generate authorization tokens to make requests


Freelancer

Recommended Posts

I'm a newly graduated developer, and I'm working on an application to strengthen my learning. This application consists of the integration of Firebase and the Loyverse API to create a POS/POS where it is possible for 3 users to use the same store. I did all the necessary steps, created my account as a developer and created one in the application on the dashboard correctly; but I'm not able to get any response from the API either in my application terminal or in the browser console, and due to this lack of response I can't get the access token or the store ID so I can create my POS/POS. I really need help!

Part 1

import getLoyverseAppAuthToken from "./getAuthTokenAPI";

import { getStoreDetails } from "./getLoyverseStoreID";

 

//* Gerando a url de autorização completa

const generateAuthURL = (): string => {

  const clientID = process.env.LOYVERSEAPI_APP_ID!;

  const redirectURI = process.env.LOYVERSEAPI_REDIRECT_URI!;

  const scope = [

    "CUSTOMERS_READ",

    "CUSTOMERS_WRITE",

    "EMPLOYEES_READ",

    "ITEMS_READ",

    "INVENTORY_READ",

    "INVENTORY_WRITE",

    "ITEMS_WRITE",

    "MERCHANT_READ",

    "PAYMENT_TYPES_READ",

    "POS_DEVICES_READ",

    "POS_DEVICES_WRITE",

    "RECEIPTS_READ",

    "RECEIPTS_WRITE",

    "SHIFTS_READ",

    "STORES_READ",

    "SUPPLIERS_READ",

    "SUPPLIERS_WRITE",

    "TAXES_READ",

    "TAXES_WRITE",

  ].join("");

  const state = "some_random_state";

  console.log('https://api.loyverse.com/oauth/authorize?client_id=${clientID}&scope=${encodeURIComponent(scope)}&response_type=code&redirect_uri=${encodeURIComponent(redirectURI)}&state=${state}')

  return `https://api.loyverse.com/oauth/authorize?client_id=${clientID}&scope=${encodeURIComponent(scope)}&response_type=code&redirect_uri=${encodeURIComponent(redirectURI)}&state=${state}`;

};

const atuhURL = generateAuthURL()

console.log('Authorization URL:', atuhURL)

 

//* Usando o código de autorização recebido no redirecionamento

const authorizationCode = "AUTHORIZATION_CODE_RECEIVED_FROM_REDIRECT";

 

const initAuthAPIFlow = async () => {

  try {

    const authTokenData = await getLoyverseAppAuthToken(authorizationCode);

    const accessAuthToken = authTokenData.access_token;

 

    console.log("Access token obtained:", accessAuthToken);

 

    const storeDetails = await getStoreDetails(accessAuthToken);

    console.log("Store details", storeDetails);

  } catch (error) {

    console.error(error);

  }

};

initAuthAPIFlow();


Part 2
 

import axios from "axios";

import { getStoreDetails } from "./getLoyverseStoreID";

 

//* Obtem o token de atutorização da LoversePOS API

const getLoyverseAppAuthToken = async (authorizationCode: string): Promise<any> => {

  const tokenURL = "https://api.loyverse.com/oauth/token";

  const appID = process.env.LOYVERSEAPI_APP_ID!;

  const appSECRET = process.env.LOYVERSEAPI_APP_SECRET!;

  //!!!!TROCAR DEPOIS DO DEPLOY NÃO ESQUECER

  const redirectURI = process.env.LOYVERSEAPI_REDIRECT_URI!;

 

  const requestParams = new URLSearchParams();

  requestParams.append("client_id", appID)

  requestParams.append("client_secret", appSECRET)

  requestParams.append("redirect_uri",redirectURI)

  requestParams.append("code", authorizationCode)

  requestParams.append("grant_type", "authorization_code")

 

  try {

    const res = await axios.post(tokenURL, requestParams, {

        headers: {

          "Content-Type": "application/x-www-form-urlencoded",

        },

      }

    );

    return res.data;

  } catch (error) {

    console.error("Error fetching access token:", error);

    throw error;

  }

};


 

export default getLoyverseAppAuthToken

Part 3

 

import api from "./api";

 

//* Obtem os detalhes da loja para interação com LoyversePOS API

export const getStoreDetails = async (accessToken: string | any): Promise<string | number> => {

  const tokenLoyverseAuth = process.env.LOYVERSEAPI_APP_SECRET;

 

  try {

    const res = await api.get("/stores", {

      headers: {

        Authorization: `Bearer ${tokenLoyverseAuth}`

      }

    });

    console.log(res.data)

    return res.data;

 

  } catch (error) {

    console.error("Error fetching store details", error);

    throw error

  }

};

 

Link to comment
Share on other sites

Hello! Did you made sure that your redirect_uri is catching the response code? When you placed the authorization url on your app, did the application directed you to the Loyverse authorization log in page to log in user credentials in order to allow the scopes defined to access of data in the user's account?  If you truly did call from our API then a response should inevitably appear on our side. Perhaps your application is not able to catch the responses and thus not showing in your terminal nor is it appearing in the console. I recommend either set up a way to also intercept the data to check and see if indeed requests from your application is being sent to our API and a response is being returned as well.

Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Loyverse Point of Sale

 

 

 

 

×
×
  • Create New...