Sample code for 30+ languages & platforms
Xbase++

Refresh Access Token on 401 Unauthorized and Retry (Service Account)

See more Google Cloud Storage Examples

Demonstrates how to handle an expired access token error, refresh the token, and retry the request. (In this case, the request is to download an object from Google Cloud Storage.)

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oSbToken
LOCAL oHttp
LOCAL cUrl
LOCAL oFileData
LOCAL nResponseCode
LOCAL oFac
LOCAL cJsonKey
LOCAL oGAuth
LOCAL oTlsSock
LOCAL oSbErrorResponse

nSuccess := 0

//  This example requires the Chilkat API to have been previously unlocked.
//  See Global Unlock Sample for sample code.

//  This example uses a previously obtained access token having permission for the 
//  scope "https://www.googleapis.com/auth/cloud-platform"

//  In this example, Get Google Cloud Storage OAuth2 Access Token, 
//  the service account access token was saved to a text file.  This example fetches the access token from the file..
oSbToken := CreateObject("Chilkat.StringBuilder")
nSuccess := oSbToken:LoadFile("qa_data/tokens/googleCloudStorageAccessToken.txt", "utf-8")
IF (nSuccess == 0)
    ? "Failed to load access token."
    oSbToken:destroy()
    RETURN
ENDIF

oHttp := CreateObject("Chilkat.Http")

oHttp:AuthToken := oSbToken:GetAsString()

//  Construct a URL to download an object named "starfish.jpg" from the "chilkat-ocean" bucket.
oHttp:SetUrlVar("bucket_name", "chilkat-ocean")
oHttp:SetUrlVar("object_name", "starfish.jpg")
cUrl := "https://www.googleapis.com/storage/v1/b/{$bucket_name}/o/{$object_name}?alt=media"

//  If there is an error response, then we didn't actually download the file data,
//  but instead we downloaded an error response..
oFileData := CreateObject("Chilkat.BinData")
nSuccess := oHttp:DownloadBd(cUrl, oFileData)
nResponseCode := oHttp:LastStatus
IF ((nSuccess == 0) .AND. (nResponseCode != 401))
    ? oHttp:LastErrorText
    oSbToken:destroy()
    oHttp:destroy()
    oFileData:destroy()
    RETURN
ENDIF

IF (nResponseCode == 401)
    ? "Received 401 Unauthorized response. Attempting to refresh the access token..."

    //  May be that the access token expired.
    //  Load our JSON key and request a new access token, then retry the original request.
    oFac := CreateObject("Chilkat.FileAccess")
    cJsonKey := oFac:ReadEntireTextFile("qa_data/googleApi/ChilkatCloud-13a07a2e8b3f.json", "utf-8")
    IF (oFac:LastMethodSuccess != 1)
        ? oFac:LastErrorText
        oSbToken:destroy()
        oHttp:destroy()
        oFileData:destroy()
        oFac:destroy()
        RETURN
    ENDIF

    oGAuth := CreateObject("Chilkat.AuthGoogle")
    oGAuth:JsonKey := cJsonKey
    oGAuth:Scope := "https://www.googleapis.com/auth/cloud-platform"
    oGAuth:ExpireNumSeconds := 3600
    oGAuth:SubEmailAddress := ""

    oTlsSock := CreateObject("Chilkat.Socket")
    nSuccess := oTlsSock:Connect("www.googleapis.com", 443, 1, 5000)
    IF (nSuccess != 1)
        ? oTlsSock:LastErrorText
        oSbToken:destroy()
        oHttp:destroy()
        oFileData:destroy()
        oFac:destroy()
        oGAuth:destroy()
        oTlsSock:destroy()
        RETURN
    ENDIF

    nSuccess := oGAuth:ObtainAccessToken(oTlsSock)
    IF (nSuccess != 1)
        ? oGAuth:LastErrorText
        oSbToken:destroy()
        oHttp:destroy()
        oFileData:destroy()
        oFac:destroy()
        oGAuth:destroy()
        oTlsSock:destroy()
        RETURN
    ENDIF

    oFac:WriteEntireTextFile("qa_data/tokens/googleCloudStorageAccessToken.txt", oGAuth:AccessToken, "utf-8", 0)

    //  Retry the original request.
    oHttp:AuthToken := oGAuth:AccessToken
    nSuccess := oHttp:DownloadBd(cUrl, oFileData)
    IF (nSuccess == 0)
        ? oHttp:LastErrorText
        oSbToken:destroy()
        oHttp:destroy()
        oFileData:destroy()
        oFac:destroy()
        oGAuth:destroy()
        oTlsSock:destroy()
        RETURN
    ENDIF

ENDIF

IF (nResponseCode != 200)
    //  Get the error response
    oSbErrorResponse := CreateObject("Chilkat.StringBuilder")
    oSbErrorResponse:AppendBd(oFileData, "utf-8", 0, 0)
    ? "Error response code = " + Str(nResponseCode)
    ? "Error:"
    ? oSbErrorResponse:GetAsString()
    oSbToken:destroy()
    oHttp:destroy()
    oFileData:destroy()
    oFac:destroy()
    oGAuth:destroy()
    oTlsSock:destroy()
    oSbErrorResponse:destroy()
    RETURN
ENDIF

? "Success."

//  Save the downloaded data to a file.
nSuccess := oFileData:WriteFile("qa_output/starfish.jpg")

oSbToken:destroy()
oHttp:destroy()
oFileData:destroy()
oFac:destroy()
oGAuth:destroy()
oTlsSock:destroy()
oSbErrorResponse:destroy()