Sample code for 30+ languages & platforms
PureBasic

Configure OAuth 1.0a Authentication for REST

See more REST Examples

Demonstrates Rest.SetAuthOAuth1, which associates an OAuth1 provider so Chilkat computes the OAuth 1.0/1.0a signature for each request. The second argument selects whether OAuth parameters are placed in the query string or the Authorization header.

Background. OAuth 1.0a signs each request using the consumer key/secret and the access token/secret. Chilkat generates the nonce, timestamp, and signature automatically for every request sent through the object.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkRest.pb"
IncludeFile "CkOAuth1.pb"

Procedure ChilkatExample()

    success.i = 0

    rest.i = CkRest::ckCreate()
    If rest.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    bTls.i = 1
    bAutoReconnect.i = 1
    success = CkRest::ckConnect(rest,"example.com",443,bTls,bAutoReconnect)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        ProcedureReturn
    EndIf

    ;  Configure OAuth 1.0a request signing.  Provide the consumer credentials and the access token
    ;  credentials; Chilkat computes the OAuth signature for each request.
    oauth1.i = CkOAuth1::ckCreate()
    If oauth1.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  Normally you would not hard-code secrets in source.  You should instead obtain them from an
    ;  interactive prompt, environment variable, or a secrets vault.
    CkOAuth1::setCkConsumerKey(oauth1, "CONSUMER_KEY")
    CkOAuth1::setCkConsumerSecret(oauth1, "CONSUMER_SECRET")
    CkOAuth1::setCkToken(oauth1, "ACCESS_TOKEN")
    CkOAuth1::setCkTokenSecret(oauth1, "ACCESS_TOKEN_SECRET")
    CkOAuth1::setCkSignatureMethod(oauth1, "HMAC-SHA1")

    ;  The 2nd argument selects whether OAuth parameters are placed in the query string (1) or the
    ;  Authorization header (0).
    bUseQueryParams.i = 0
    success = CkRest::ckSetAuthOAuth1(rest,oauth1,bUseQueryParams)
    If success = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth1::ckDispose(oauth1)
        ProcedureReturn
    EndIf

    responseText.s = CkRest::ckFullRequestNoBody(rest,"GET","/api/resource")
    If CkRest::ckLastMethodSuccess(rest) = 0
        Debug CkRest::ckLastErrorText(rest)
        CkRest::ckDispose(rest)
        CkOAuth1::ckDispose(oauth1)
        ProcedureReturn
    EndIf

    Debug responseText


    CkRest::ckDispose(rest)
    CkOAuth1::ckDispose(oauth1)


    ProcedureReturn
EndProcedure