twitter_oauthbodyhash.c

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值