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

Walmart Partner API Authentication (Generate a Signature for a Request)

See more RSA Examples

Demonstrates how to generate a signature for a Walmart Partner REST API call.

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL cConsumerId
LOCAL cBaseUrl
LOCAL cPrivateEncodedStr
LOCAL cHttpMethod
LOCAL oDt
LOCAL nBLocal
LOCAL nTimeStampVal
LOCAL oSbStringToSign
LOCAL oPrivKey
LOCAL oRsa
LOCAL cSignatureString

nSuccess := 0

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

cConsumerId := "b68d2a72...."
cBaseUrl := "https://marketplace.walmartapis.com/v2/feeds"
//  This is your Base64 encoded private key
cPrivateEncodedStr := "MIICeAIBADANBgkqhkiG9w0BAQEFAA......"
cHttpMethod := "GET"

//  We need a timestamp in decimal string form representing the number of milliseconds since Jan 01 1970 UTC.
oDt := CreateObject("Chilkat.CkDateTime")
//  Set bLocal = 1 for a timestamp in the local timezone.  Set bLocal = 0 for a UTC timestamp.
nBLocal := 0
//  This gets the timestamp in seconds, not milliseconds.
nTimeStampVal := oDt:GetAsUnixTime(nBLocal)

//  Build the string to sign.
oSbStringToSign := CreateObject("Chilkat.StringBuilder")
oSbStringToSign:Append(cConsumerId)
oSbStringToSign:Append(Chr(10))
oSbStringToSign:Append(cBaseUrl)
oSbStringToSign:Append(Chr(10))
oSbStringToSign:Append(cHttpMethod)
oSbStringToSign:Append(Chr(10))
oSbStringToSign:AppendInt(nTimeStampVal)
//  We add three zero's so that the timestamp value is in milliseconds.
//  We don't care about accuracy down to less than a second.
//  All the server cares about is that the request was signed at the current date/time
//  within some reasonable margin of error (to account for systems having clocks
//  that may be slightly different).
oSbStringToSign:Append("000" + Chr(10))

oPrivKey := CreateObject("Chilkat.PrivateKey")
//  Load the private key into a private key object.
//  Note: Technically the private key is not PEM because it lacks the header/footer strings
//  used for PEM.  However, the LoadPem method will still accept it and load it correctly.
nSuccess := oPrivKey:LoadPem(cPrivateEncodedStr)
IF (nSuccess == 0)
    ? oPrivKey:LastErrorText
    oDt:destroy()
    oSbStringToSign:destroy()
    oPrivKey:destroy()
    RETURN
ENDIF

oRsa := CreateObject("Chilkat.Rsa")
nSuccess := oRsa:UsePrivateKey(oPrivKey)
IF (nSuccess == 0)
    ? oRsa:LastErrorText
    oDt:destroy()
    oSbStringToSign:destroy()
    oPrivKey:destroy()
    oRsa:destroy()
    RETURN
ENDIF

//  We want a base64 signature string.
oRsa:EncodingMode := "base64"

cSignatureString := oRsa:SignStringENC(oSbStringToSign:GetAsString(), "SHA256")
IF (oRsa:LastMethodSuccess == 0)
    ? oRsa:LastErrorText
    oDt:destroy()
    oSbStringToSign:destroy()
    oPrivKey:destroy()
    oRsa:destroy()
    RETURN
ENDIF

? "Signature String: " + cSignatureString

oDt:destroy()
oSbStringToSign:destroy()
oPrivKey:destroy()
oRsa:destroy()