Authorization in Go SDK

This documentation provides an overview of how to configure and authorize requests to the Aspose Barcode Cloud API using JWT and external authorization methods. It covers key code examples and settings needed to enable secure and authorized API requests.

Note: For manage authorization you should have a Client Id and Client Secret. How to get it described in Quick Start Section

Overview

Aspose Barcode Cloud API supports two main authorization methods:

  • JWT (JSON Web Token) - Allows token-based authentication where tokens are obtained using Client Id and Client Secret.
  • External Authorization - Permits manual setting of JWT tokens for custom authorization.

JWT Authorization

When using JWT, the API classes automatically obtains a token and includes it in each API request.

Setting Up JWT Authorization

To set up JWT authorization, specify the ClientID and ClientSecret:

jwtConf := jwt.NewConfig(
		"<Your-Client-Id>",
		"<Your-Client-Secret>",
	)
	authCtx := context.WithValue(context.Background(),
		barcode.ContextJWT,
		jwtConf.TokenSource(context.Background()))

	client := barcode.NewAPIClient(barcode.NewConfiguration())
    //Call client.GenerateAPI.BarcodeGenerateBarcodeTypeGet(authCtx, type, data, opts) and so on.

Upon setup, the internal SDK methods handles token generation and token refresh:

  • The RequestToken method requests a token using ClientID and ClientSecret.
  • The token is added to the Authorization header for each request.

External Authorization

External authorization allows you to manually manage tokens. Assign your JWT token directly to AccessToken and leave ClientID and ClientSecret blank.

Setting Up External Authorization

Example configuration with External Authorization:


        config := barcode.NewConfiguration()
		config.AddDefaultHeader("Authorization", "Bearer "+"<Your-External-Jwt-Token>")
		client := barcode.NewAPIClient(config)
        authCtx := context.Background()

    //Call client.RecognizeAPI.BarcodeRecognizeBodyPost(authCtx, base64Request) and son on

In this mode, the API sends the provided JWT in the Authorization header without attempting to generate or refresh the token.

How to Fetch a Token without SDK Internal Methods

To fetch a new token, send a POST request to https://id.aspose.cloud/connect/token with the application/x-www-form-urlencoded content type. In the request body, specify the following parameters:

grant_type=client_credentials
client_id=<Your-Client-Id>
client_secret=<Your-Client-Secret>

A Code example for fetching token:

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"strings"
)

func main() {
	clientID := "Client Id from https://dashboard.aspose.cloud/applications"
	clientSecret := "Client Secret from https://dashboard.aspose.cloud/applications"

	if strings.HasPrefix(clientID, "Client Id") {
		fmt.Println("clientID not configured. Skip this snippet test")
		return
	}

	baseURL := "https://id.aspose.cloud/"
	endpoint := "connect/token"

	payload := []byte(fmt.Sprintf(
		"grant_type=client_credentials&client_id=%s&client_secret=%s",
		clientID, clientSecret,
	))

	req, err := http.NewRequest("POST", baseURL+endpoint, bytes.NewBuffer(payload))
	if err != nil {
		log.Fatalln("Error creating request: %v\n", err)
	}

	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Fatalln("HTTP request error: %v\n", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		log.Fatalln("HTTP error occurred: %v\n", resp.Status)
	}

	var data map[string]interface{}
	if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
		log.Fatalln("Error decoding response: %v\n", err)
	}

	fmt.Println("Token reciewed successfully")
	// To view token uncomment next line
	// fmt.Println(data["access_token"])

}

Conclusion

Configuring authorization in the Aspose.BarCode.Cloud SDK for Go allows secure, authenticated access to barcode-related functionalities. Choose JWT authorization wiht client id and secret for automatic token management or external for custom token handling.

With these examples and explanations, you should be able to set up authorization for the Aspose.BarCode.Cloud SDK for Go.