-
Notifications
You must be signed in to change notification settings - Fork 0
/
libplurkapic.c
465 lines (403 loc) · 14.1 KB
/
libplurkapic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "libplurkapic.h"
#include <stdarg.h>
#include <string.h>
const char* plurk_url = "http://www.plurk.com/";
const char* plurk_uri_request = "OAuth/request_token";
const char* plurk_uri_access = "OAuth/access_token";
const char* plurk_uri_verification = "http://www.plurk.com/OAuth/authorize?oauth_token=";
const char* plurk_uri_post = "APP/Timeline/plurkAdd";
inline static char* s_concate(const char**, const char**);
inline static int get_keysecret(const char*, char**, char**);
inline static int get_response_token(char*, char*, char**, char**);
inline static int get_verifier(char*, char*, char*, char*, char**);
inline static int get_access_token(char*, char*, char**, char**, char*);
inline static void encode(const char *s, char *enc, char *tb);
inline static char* urlencode(const char* raw);
// reference from:
// http://www.cnblogs.com/hoodlum1980/archive/2012/05/28/2521500.html
int URLEncode(const char* str, const int strSize, char* result, const int resultSize)
{
int i;
int j = 0;//for result index
char ch;
if ((str==NULL) || (result==NULL) || (strSize<=0) || (resultSize<=0)) {
return 0;
}
for ( i=0; (i<strSize)&&(j<resultSize); ++i) {
ch = str[i];
if (((ch>='A') && (ch<'Z')) ||
((ch>='a') && (ch<'z')) ||
((ch>='0') && (ch<'9'))) {
result[j++] = ch;
} else if (ch == ' ') {
result[j++] = '+';
} else if (ch == '.' || ch == '-' || ch == '_' || ch == '*') {
result[j++] = ch;
} else {
if (j+3 < resultSize) {
sprintf(result+j, "%%%02X", (unsigned char)ch);
j += 3;
} else {
return 0;
}
}
}
result[j] = '\0';
return j;
}
/**
* @brief string concate
*
* @param p1 address of string1
* @param p2 address of string2
*
* @return address of newstring
*/
inline static char* s_concate(const char** p1, const char** p2)
{
size_t len1 = strlen(*p1);
size_t len2 = strlen(*p2);
size_t len_newstring = len1 + len2;
char* newstring = malloc(sizeof(char) * (len_newstring + 1));
memset(newstring, '\0', len_newstring + 1);
newstring = strncpy(newstring, *p1, len1);
newstring = strncat(newstring, *p2, len2);
return newstring;
}
/**
* @brief static function for parse the key/secret info from raw data
*
* @param raw raw data
* @param key key
* @param secret secret
*
* @return error on 1
*/
inline static int get_keysecret(const char* raw,
char** key,
char** secret)
{
int rc;
char **rv = NULL;
if (*key) free(*key);
if (*secret) free(*secret);
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG: HTTP-reply: %s\n",raw);
#endif
rc = oauth_split_url_parameters(raw, &rv);
//qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
if( rc>=2
&& !strncmp(rv[0],"oauth_token_secret=",18)
&& !strncmp(rv[1],"oauth_token=",11)
){
*key=strdup(&(rv[1][12]));
*secret=strdup(&(rv[0][19]));
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG: rv1: '%s'\trv0: '%s'\n", rv[1],rv[0]);
printf("SAMUEL_DEBUG: key: '%s'\tsecret: '%s'\n",*key, *secret);
#endif
}
if(rv) free(rv);
return 0;
}
/**
* @brief Use API key/secret to retrive the resonse key/secret
*
* @param req_key API key
* @param req_secret API secret key
* @param res_key the address of the response key
* @param res_secret the address of the response secret key
*
* @return error on 1
*/
inline static int get_response_token(char* req_key,
char* req_secret,
char** res_key,
char** res_secret){
int fail = 0;
char *request_token_uri = s_concate(&(plurk_url), &(plurk_uri_request));
char *postarg = NULL;
char *req_url;
char *reply;
req_url = oauth_sign_url2(
request_token_uri, // url
&postarg, // postarg
OA_HMAC, // OAuthMethod
"POST", // HTTPMethod
req_key, // customer key
req_secret, // customer secret
NULL, // token key
NULL); // token secret
reply = oauth_http_post(req_url,postarg);
if (!reply){
printf("HTTP request for an oauth request-token failed.\n");
return (fail = 1);
}
else
fail = get_keysecret(reply, res_key, res_secret);
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG, res_key: '%s'\t res_secret: '%s'\n", *res_key, *res_secret);
#endif
if(reply) free(reply);
if(req_url) free(req_url);
if(postarg) free(postarg);
if(request_token_uri) free(request_token_uri);
return fail;
}
/**
* @brief Use API key, response key-pair and verification code
* to retrive the permanent key-pair
*
* @param req_key API key
* @param req_secret API secret key
* @param res_key the address of the response key
* @param res_secret the address of the response secret key
* @param verifier verification code
*
* @return error on 1
*/
inline static int get_access_token(char* req_key,
char* req_secret,
char** res_key,
char** res_secret,
char* verifier){
int fail = 0;
char *request_token_uri = s_concate(&(plurk_url), &(plurk_uri_access));
char *postarg = NULL;
char *req_url;
char *reply;
int verifier_len = strlen(verifier);
char *verifier_perm = malloc(sizeof(char)*(15 + verifier_len + 1));
memset(verifier_perm, 0, 15 + verifier_len + 1);
verifier_perm = memcpy(verifier_perm, "oauth_verifier=", 15);
verifier_perm = strncat(verifier_perm, verifier, verifier_len);
//req_url = oauth_sign_url2(request_token_uri, // const char *url
// &postarg, // char **postarg
// OA_HMAC, // OAuthMethod method
// "POST", // const char *http_method
// req_key, // const char *key
// req_secret, // const char *secret
// res_t_key, // const char *t_key
// res_t_secret // char *t_secret
// );
// transfer oauth_sign_url2() in steps
// example edited from:
// http://liboauth.sourceforge.net/tests_2oauthtest2_8c-example.html#a0
int argc=0;
int i;
char **argv=NULL;
char *req_hdr = NULL;
char *http_hdr= NULL;
argc = oauth_split_url_parameters(request_token_uri, &argv);
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG, before add parameters to array\n");
for (i=0;i<argc; i++)
printf("%d:%s\n",i,argv[i]);
#endif
// the most important step here!! add parameter
oauth_add_param_to_array(&argc, &argv, verifier_perm);
#if SAMUEL_DEBUG
printf("====\nSAMUEL_DEBUG, after add parameters to array\n");
for (i=0;i<argc; i++)
printf("%d:%s\n",i,argv[i]);
#endif
//do something important here??
oauth_sign_array2_process(&argc, &argv,
NULL, //< postargs (unused)
OA_HMAC,
"POST", //< HTTP method (defaults to "GET")
req_key, req_secret,//NULL, NULL);
*res_key, *res_secret);
req_hdr = oauth_serialize_url_sep(argc, 1, argv, ", ", 6);
req_url = oauth_serialize_url_sep(argc, 0, argv, "&", 1);
oauth_free_array(&argc, &argv);
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG, req_hdr: %s\n",req_hdr);
printf("SAMUEL_DEBUG, req_url: %s\n",req_url);
#endif
http_hdr = malloc(strlen(req_hdr) + 100);
memset(http_hdr,0,100);
sprintf(http_hdr, "Authorization: OAuth realm=\"\", %s", req_hdr);
#if SAMUEL_DEBUG
printf("request URL=%s\n", req_url);
printf("request header=%s\n\n", http_hdr);
#endif
// POST HTTPMethod
reply = oauth_http_post2(req_url,&postarg,http_hdr);
// GET HTTPMethod
//reply = oauth_http_get2(req_url,postarg,http_hdr);
if(!reply){
printf("SAMUEL_DEBUG, HTTP request for an oauth request-token failed.\n");
return (fail = 1);
}
else
fail = get_keysecret(reply, res_key, res_secret);
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG, res_key: '%s'\t res_secret: '%s'\n", *res_key, *res_secret);
printf("Samuel_DEBUG, final %x\n",res_key);
printf("SAMUEL_DEBUG, postarg: %s\n",postarg);
#endif
if(http_hdr) free(http_hdr);
if(req_hdr) free(req_hdr);
if(verifier_perm) free(verifier_perm);
if(reply) free(reply);
if(req_url) free(req_url);
if(postarg) free(postarg);
if(request_token_uri) free(request_token_uri);
return fail;
}
/**
* @brief Use API key pair and response key pair to generate the link to
* authorize page to retrive the verification code
*
* @param req_key API key
* @param req_secret API secret key
* @param res_key response key
* @param res_secret response secret key
* @param verifier the address of the verification code
*
* @return error on 1
*/
inline static int get_verifier(char* req_key,
char* req_secret,
char* res_key,
char* res_secret,
char** verifier){
*verifier = malloc(sizeof(char) * 8);
printf("verification link:\n '%s%s' \n", plurk_uri_verification, res_key);
memset(*verifier, '\0', 8);
scanf("%s",*verifier);
return 0;
}
/**
* @brief Use given API key to get the permanent key pair as the
* initialization step of plurk
*
* @param req the address of API key pair
* @param permanent the address of permanent key pair
*
* @return error on 1
*/
int plurk_init(key_pair* req, key_pair* permanent){
// prepare for response key
key_pair* tmp = malloc(sizeof(key_pair));
tmp->key = NULL;
tmp->secret = NULL;
// prepare for authorize verified code
char* verifier_code = malloc(sizeof(char) * 8);
// OAuth prcess procedure
// 1. get response key
// 2. get the verifier code
// 3. get the permanent access key
get_response_token(req->key, req->secret, &(tmp->key), &(tmp->secret));
get_verifier(req->key, req->secret, tmp->key, tmp->secret, &verifier_code);
get_access_token(req->key, req->secret, &(tmp->key), &(tmp->secret),
verifier_code);
// store the permanent key/secret
permanent->key = tmp->key;
permanent->secret = tmp->secret;
// free the dynamic allocated space
free(verifier_code);
//free(tmp->key); // << save for outer, no free
//free(tmp->secret); // << save for outer, no free
return 0;
}
int plurk_api( key_pair* request
,key_pair* permanent
,const char* api_uri
,const char* invoke_method
,int arg_count, ...
)
{
char* request_uri = s_concate(&(plurk_url), &(plurk_uri_request));
char* api_url = s_concate( &(plurk_url) ,&(api_uri));
int argc = 0;
int i = 0;
char** argv = NULL;
char* req_hdr = NULL;
char* req_url = NULL;
char* http_hdr = NULL;
char* reply = NULL;
char* postarg = NULL;
argc = oauth_split_url_parameters(api_url, &argv);
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG, before add parameters to array\n");
for (i=0;i<argc; i++)
printf("%d:%s\n",i,argv[i]);
#endif
int arg_index;
char* arg_from_valist;
va_list vaarg;
va_start(vaarg, arg_count);
for (arg_index = 0; arg_index < arg_count; arg_index++) {
arg_from_valist = va_arg(vaarg, char*);
oauth_add_param_to_array(&argc, &argv, arg_from_valist);
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG, arg_from_valist: %s\n", arg_from_valist);
printf("SAMUEL_DEBUG, argc:%d\n", argc);
#endif
}
va_end(vaarg);
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG, before add parameters to array\n");
for (i=0;i<argc; i++)
printf("%d:%s\n",i,argv[i]);
#endif
oauth_sign_array2_process(&argc, &argv,
NULL, //< postargs (unused)
OA_HMAC,
"POST", //< HTTP method (defaults to "GET")
request->key, request->secret,//NULL, NULL);
permanent->key, permanent->secret);
req_hdr = oauth_serialize_url_sep(argc, 1, argv, ", ", 100);
req_url = oauth_serialize_url_sep(argc, 0, argv, "&", 1);
oauth_free_array(&argc, &argv);
#if SAMUEL_DEBUG
printf("SAMUEL_DEBUG, req_hdr: %s\n", req_hdr);
printf("SAMUEL_DEBUG, req_url: %s\n", req_url);
#endif
http_hdr = malloc(strlen(req_hdr) + 200);
memset(http_hdr,0,100);
sprintf(http_hdr, "Authorization: OAuth realm=\"\", %s", req_hdr);
#if SAMUEL_DEBUG
printf("request URL=%s\n", req_url);
printf("request header=%s\n\n", http_hdr);
#endif
reply = oauth_http_post2(req_url, &postarg, http_hdr);
if(!reply){
printf("SAMUEL_DEBUG, HTTP request for an oauth request-token failed.\n");
}
else
printf("SAMUEL_DEBUG, reply: %s\n", reply);
if (reply) free(reply);
if (req_hdr) free(req_hdr);
if (req_url) free(req_url);
if (http_hdr) free(http_hdr);
if (postarg) free(postarg);
if (api_url) free(api_url);
return 0;
}
int plurk_post( key_pair* request
,key_pair* permanent
,const char* invoke_method
,const char* content
,const char* qualifier
)
{
char* post_qualifier_h ="qualifier=";
char* post_content_h ="content=";
//char* buff = malloc(sizeof(char)*201);
//memset(buff, '\0', 201);
//URLEncode(content, strlen(content), buff, 201);
char* post_qualifier = s_concate(&(post_qualifier_h), &(qualifier));
char* post_content = s_concate(&(post_content_h), &(content));
plurk_api( request
,permanent
,plurk_uri_post // << API_URI
,invoke_method // << RESTful method
,2 // << arg_count
,post_content
,post_qualifier // << ...
);
return 0;
}