Sample code for 30+ languages & platforms
Go Requires Chilkat v11.0.0+

PayPal: Get a sandbox access token

See more PayPal Examples

Exchange a sandbox REST app client ID and secret for a bearer token on a merchant-controlled backoffice workstation. PayPal supplies its lifetime in seconds. Store the token in a protected credential store; the companion expiry example reads only timing metadata.

PayPal documentation

Chilkat Go Downloads

Go
    success := false

    //  See Unlock Chilkat before making requests.
    http := chilkat.NewHttp()
    //  Load these values from the credential store on the merchant backoffice workstation.
    //  Never embed the merchant secret in an app distributed to customers.
    http.SetLogin("SANDBOX_CLIENT_ID")
    http.SetPassword("SANDBOX_CLIENT_SECRET")
    http.SetBasicAuth(true)
    issued := chilkat.NewCkDateTime()
    issued.SetFromCurrentSystemTime()
    resp := chilkat.NewHttpResponse()
    success = http.HttpStr("POST","https://api-m.sandbox.paypal.com/v1/oauth2/token","grant_type=client_credentials","utf-8","application/x-www-form-urlencoded",resp)
    if success != true {
        fmt.Println("No HTTP response: ", http.LastErrorText())
        http.DisposeHttp()
        issued.DisposeCkDateTime()
        resp.DisposeHttpResponse()
        return
    }

    if (resp.StatusCode() < 200) || (resp.StatusCode() >= 300) {
        errorInfo := chilkat.NewJsonObject()
        errorInfo.Load(resp.BodyStr())
        fmt.Println("HTTP ", resp.StatusCode(), " ", *errorInfo.StringOf("error"), " ", *errorInfo.StringOf("error_description"))
        fmt.Println("PayPal debug ID: ", *errorInfo.StringOf("debug_id"))
        http.DisposeHttp()
        issued.DisposeCkDateTime()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        return
    }

    result := chilkat.NewJsonObject()
    parsed := result.Load(resp.BodyStr())
    if parsed != true {
        fmt.Println("Expected a JSON response; HTTP ", resp.StatusCode())
        http.DisposeHttp()
        issued.DisposeCkDateTime()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        return
    }

    accessToken := result.StringOf("access_token")
    lifetime := result.IntOf("expires_in")
    if (result.HasMember("access_token") != true) || (lifetime <= 0) {
        fmt.Println("Missing token or lifetime.")
        http.DisposeHttp()
        issued.DisposeCkDateTime()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        return
    }

    //  Supply accessToken to Http.AuthToken. Never log the bearer token or client secret.
    //  Save only timing metadata here, relative to the start of the token request.
    timing := chilkat.NewJsonObject()
    timing.UpdateString("issued_at",*issued.GetAsTimestamp(false))
    timing.UpdateInt("expires_in",lifetime)
    savedTiming := timing.WriteFile("paypal-token-timing.json")
    if savedTiming != true {
        fmt.Println("Could not save token timing.")
        http.DisposeHttp()
        issued.DisposeCkDateTime()
        resp.DisposeHttpResponse()
        errorInfo.DisposeJsonObject()
        result.DisposeJsonObject()
        timing.DisposeJsonObject()
        return
    }

    fmt.Println("Token received; lifetime in seconds: ", lifetime)

    http.DisposeHttp()
    issued.DisposeCkDateTime()
    resp.DisposeHttpResponse()
    errorInfo.DisposeJsonObject()
    result.DisposeJsonObject()
    timing.DisposeJsonObject()