#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <oauth.h>
int my_data_post(char *url, char *data) {
const char *c_key = "key"; //< consumer key
const char *c_secret = "secret"; //< consumer secret
char *t_key = "tkey"; //< access token key
char *t_secret = "tsecret"; //< access token secret
char *postarg = NULL;
char *req_url = NULL;
char *reply = NULL;
char *bh;
char *uh;
char *sig_url;
bh=oauth_body_hash_data(strlen(data), data);
uh = oauth_catenc(2, url, bh);
req_url = oauth_sign_url2(uh, &postarg, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
printf("POST: %s?%s/n", req_url, postarg);
if (uh) free(uh);
sig_url = malloc(2+strlen(req_url)+strlen(postarg));
sprintf(sig_url,"%s?%s",req_url, postarg);
reply = oauth_post_data(sig_url, data, strlen(data), "Content-Type: application/json");
if(sig_url) free(sig_url);
printf("REPLY: %s/n", reply);
if(reply) free(reply);
return 0;
}
int main (int argc, char **argv) {
char *base_url = "http://localhost/oauthtest.php";
char *teststring="Hello World!";
/* TEST_BODY_HASH_FILE and TEST_BODY_HASH_DATA are only
* here as examples and for testing during development.
*
* the my_data_post() function above uses oauth_body_hash_data()
*/
#if defined TEST_BODY_HASH_FILE || defined TEST_BODY_HASH_DATA
char *bh=NULL;
#endif
#ifdef TEST_BODY_HASH_FILE // example hash file
char *filename="/tmp/test";
bh=oauth_body_hash_file(filename);
if (bh) printf("%s/n", bh);
if (bh) free(bh);
#endif
#ifdef TEST_BODY_HASH_DATA // example hash data
bh=oauth_body_hash_data(strlen(teststring), teststring);
if (bh) printf("%s/n", bh);
if (bh) free(bh);
#endif
my_data_post(base_url, teststring);
return(0);
}
twitter_oauthbodyhash.c
最新推荐文章于 2024-09-15 22:55:21 发布