Access
Connect cross-platform accounts & identity management
You can use AccelByte Cloud’s Golang SDK to implement our backend services within your game. The SDK acts as a bridge between your game and our services. This guide will show you how to create an application that uses Golang Server SDK from scratch.
AB_BASE_URL
environment variable.Confidential
.
Client ID
value for AB_CLIENT_ID
environment variable.Client Secret
value for AB_CLIENT_SECRET
environment variable.Create a folder and use go mod init
to create a Golang project.
$ mkdir myproject
$ cd myproject/
$ go mod init golang-application
Add the required configuration in go.mod
.
Replace {VERSION}
with a specific release version tag (opens new window)
Run go mod tidy
.
module golang-application
go 1.16
require (
github.com/AccelByte/accelbyte-go-sdk {VERSION}
)
TIP
We recommended using the Golang Server SDK version that matches your AccelByte Cloud version.
Create an SDK instance, log in using client credentials, and call an AccelByte Cloud API in main.go
.
Create the file main.go
inside the project directory.
The ConfigRepositoryImpl
gets its values from AB_BASE_URL
, AB_CLIENT_ID
, and AB_CLIENT_SECRET
environment variables.
package main
import (
"github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclient/o_auth2_0_extension"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/utils/auth"
"github.com/sirupsen/logrus"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/factory"
"github.com/AccelByte/accelbyte-go-sdk/services-api/pkg/service/iam"
)
var (
// use the default config and token implementation
configRepo = *auth.DefaultConfigRepositoryImpl()
tokenRepo = *auth.DefaultTokenRepositoryImpl()
)
func main() {
// prepare the IAM Oauth service
oauth := &iam.OAuth20Service{
Client: factory.NewIamClient(&configRepo),
ConfigRepository: &configRepo,
TokenRepository: &tokenRepo,
}
clientId := oauth.ConfigRepository.GetClientId()
clientSecret := oauth.ConfigRepository.GetClientSecret()
// call the endpoint tokenGrantV3Short through the wrapper 'LoginClient'
err := oauth.LoginClient(&clientId, &clientSecret)
if err != nil {
logrus.Error("failed login client")
} else {
logrus.Info("successful login")
}
// get the token
token, _ := oauth.TokenRepository.GetToken()
logrus.Infof("print %v", *token.AccessToken)
// prepare the IAM's Oauth 2.0 Extention service
oAuth20ExtensionService := &iam.OAuth20ExtensionService{
Client: factory.NewIamClient(&configRepo),
TokenRepository: &tokenRepo,
}
input := &o_auth2_0_extension.GetCountryLocationV3Params{}
// call an AccelByte Cloud API e.g. GetCountryLocationV3
ok, errLoc := oAuth20ExtensionService.GetCountryLocationV3Short(input)
if errLoc != nil {
logrus.Error(errLoc.Error())
} else {
logrus.Infof("Country name: %s", *ok.CountryName)
}
}
Set the required environment variables and run the code using go run main.go
.
$ export AB_BASE_URL="https://demo.accelbyte.io" # AccelByte Cloud Base URL e.g. demo environment
$ export AB_CLIENT_ID="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # AccelByte Cloud OAuth Client ID
$ export AB_CLIENT_SECRET="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # AccelByte Cloud OAuth Client Secret
$ go run main.go
Now you’re ready to integrate any of the following AccelByte services into your application. All services have already been imported in .../pkg/service.<your-service>
will be changed to the service name you want to use automatically during the import.
See our Golang SDK example repo (opens new window) for a selection of test cases you can use to customize your game.
SDK Reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/iam-sdk/pkg/iamclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/basic-sdk/pkg/basicclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/social-sdk/pkg/socialclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/platform-sdk/pkg/platformclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/group-sdk/pkg/groupclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/cloudsave-sdk/pkg/cloudsaveclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/dsmc-sdk/pkg/dsmcclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/sessionbrowser-sdk/pkg/sessionbrowserclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/lobby-sdk/pkg/lobbyclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/achievement-sdk/pkg/achievementclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/dslogmanager-sdk/pkg/dslogmanagerclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/ugc-sdk/pkg/ugcclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/leaderboard-sdk/pkg/leaderboardclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/gdpr-sdk/pkg/gdprclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/legal-sdk/pkg/legalclientmodels"
)
SDK reference (opens new window)
import (
"github.com/AccelByte/accelbyte-go-sdk/matchmaking-sdk/pkg/matchmakingclientmodels"
)