C
C
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 C Downloads
#include <C_CkRest.h>
#include <C_CkOAuth1.h>
void ChilkatSample(void)
{
BOOL success;
HCkRest rest;
BOOL bTls;
BOOL bAutoReconnect;
HCkOAuth1 oauth1;
BOOL bUseQueryParams;
const char *responseText;
success = FALSE;
rest = CkRest_Create();
bTls = TRUE;
bAutoReconnect = TRUE;
success = CkRest_Connect(rest,"example.com",443,bTls,bAutoReconnect);
if (success == FALSE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
return;
}
// Configure OAuth 1.0a request signing. Provide the consumer credentials and the access token
// credentials; Chilkat computes the OAuth signature for each request.
oauth1 = CkOAuth1_Create();
// 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_putConsumerKey(oauth1,"CONSUMER_KEY");
CkOAuth1_putConsumerSecret(oauth1,"CONSUMER_SECRET");
CkOAuth1_putToken(oauth1,"ACCESS_TOKEN");
CkOAuth1_putTokenSecret(oauth1,"ACCESS_TOKEN_SECRET");
CkOAuth1_putSignatureMethod(oauth1,"HMAC-SHA1");
// The 2nd argument selects whether OAuth parameters are placed in the query string (TRUE) or the
// Authorization header (FALSE).
bUseQueryParams = FALSE;
success = CkRest_SetAuthOAuth1(rest,oauth1,bUseQueryParams);
if (success == FALSE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
CkOAuth1_Dispose(oauth1);
return;
}
responseText = CkRest_fullRequestNoBody(rest,"GET","/api/resource");
if (CkRest_getLastMethodSuccess(rest) == FALSE) {
printf("%s\n",CkRest_lastErrorText(rest));
CkRest_Dispose(rest);
CkOAuth1_Dispose(oauth1);
return;
}
printf("%s\n",responseText);
CkRest_Dispose(rest);
CkOAuth1_Dispose(oauth1);
}