Authentication Required for Flow gRPC (Access API)
To ensure secure access to Flow gRPC (Access API), users are required to authenticate themselves. This authentication process is necessary before utilizing any method. Quicknode endpoints consist of two crucial components: the endpoint 그리고 이에 상응하는 토큰. 사용자는 메서드 호출을 수행하기 전에 이 두 구성 요소를 사용하여 인증 자격 증명을 포함한 gRPC 구성해야 합니다.
Authentication for the Flow gRPC (Access API) can be handled in two ways:
- 기본 인증
- x-token 인증
이 문서 전반에 걸쳐, 우리는 다음 중 하나를 getAccessClientWithBasicAuth 또는 getAccessClientWithXToken 이러한 다양한 인증 메커니즘을 처리하는 방법을 보여주는 함수들입니다.
기본 인증
그 getAccessClientWithBasicAuth 이 함수는 자격 증명을 base64로 인코딩하는 기본 인증(Basic Authentication)을 사용하여 인증을 처리하는 방법을 보여줍니다. 다음은 해당 함수의 코드 구현입니다. getAccessClientWithBasicAuth 기능뿐만 아니라 basicAuth RPC 자격 증명 구현:
import (
"문맥"
"crypto/tls"
"encoding/base64"
"fmt"
"github.com/onflow/flow/protobuf/go/flow/access"
"google.golang.grpc"
"google.golang.grpc"
)
func getAccessClientWithBasicAuth(endpoint, token string) (access.AccessAPIClient, error) {
target := endpoint + ".flow-mainnet.quiknode.pro:8999" // for TLS connections
client, err := grpc.Dial(target,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(basicAuth{
username: endpoint,
password: token,
}),
)
if err != nil {
return nil, fmt.Errorf("Unable to dial endpoint %w", err)
}
return access.NewAccessAPIClient(client), nil
}
// basicAuth implements the credentials.PerRPCCredentials interface to support basic authentication for grpc requests.
type basicAuth struct {
username string
password string
}
func (b basicAuth) GetRequestMetadata(ctx context.Context, in ...string) (map[string]string, error) {
auth := b.username + ":" + b.password
enc := base64.StdEncoding.EncodeToString([]byte(auth))
return map[string]string{"authorization": "Basic " + enc}, nil
}
func (basicAuth) RequireTransportSecurity() bool {
return false
}
그 getAccessClientWithBasicAuth 이 함수는 gRPC 필요한 보안 옵션으로 구성하고, 지정된 endpoint 연결을 설정합니다. 8999. It takes the endpoint name and token as input parameters and returns an instance of the AccessAPIClient interface, which you can use to make authenticated API calls.
client, err := getAccessClientWithBasicAuth("ENDPOINT_NAME", "TOKEN")
if err != nil {
log.Fatalf("err: %v", err)
}
x-token 인증
그 getAccessClientWithXToken 이 함수는 x-token을 사용하여 인증을 처리하는 방법을 보여줍니다. 이 방법은 각 요청의 x-token 헤더에 토큰을 추가합니다.
import (
"문맥"
"crypto/tls"
"fmt"
"github.com/onflow/flow/protobuf/go/flow/access"
"google.golang.grpc"
"google.golang.grpc"
)
func getAccessClientWithXToken(endpoint, token string) (access.AccessAPIClient, error) {
target := endpoint + ".flow-mainnet.quiknode.pro:8999" // for TLS connections
client, err := grpc.Dial(target,
grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})),
grpc.WithPerRPCCredentials(auth{
token: token,
}),
)
if err != nil {
return nil, fmt.Errorf("Unable to dial endpoint %w", err)
}
return access.NewAccessAPIClient(client), nil
}
// auth implements the credentials.PerRPCCredentials interface to support x-token authentication for grpc requests.
type auth struct {
token string
}
func (a *auth) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{
"x-token": a.token,
}, nil
}
func (auth) RequireTransportSecurity() bool {
return false
}
이 방법은 기본 인증 예제와 유사하게 gRPC 구성하지만, x-token 헤더에 인증 토큰을 포함시킵니다. 이 함수를 사용하여 API 호출을 수행하는 방법은 다음과 같습니다:
client, err := getAccessClientWithXToken("ENDPOINT_NAME", "TOKEN")
if err != nil {
log.Fatalf("err: %v", err)
}