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
|
#define _POSIX_C_SOURCE 200809L
#include <err.h>
#include <string.h>
#include <jansson.h>
#include "string-util.h"
#include "auth.h"
#include "http.h"
#define CLIENT_NAME "ap_client"
char *instance_domain;
char *auth_token;
static char *protocol = "https://";
static char *app_register_url = "/api/v1/apps";
int register_app(char *instance)
{
json_t *root;
json_error_t error;
char *url;
char *req_data;
char *res_data;
root = json_pack_ex(&error, 1, "{s:s, s:s, s:s}", "client_name",
CLIENT_NAME, "redirect_uris", "urn:ietf:wg:oauth:2.0:oob", "scopes",
"read write push");
if (!root) {
fprintf(stderr, "register_app(): json pack error: line %d: %s\n",
error.line, error.text);
return -1;
}
req_data = json_dumps(root, 0);
json_decref(root);
if (!req_data) {
fprintf(stderr, "register_app(): failed to dump json\n");
return -1;
}
size_t s = strlen(protocol) + strlen(instance) + strlen(app_register_url) + 1;
url = malloc(s);
if (!url) {
err(1, "register_app(): ");
}
sprintf(url, "%s%s%s", protocol, instance, app_register_url);
res_data = post_request(url, req_data);
if (!res_data) {
free(req_data);
return -1;
}
printf("res: \n%s\n", res_data);
free(req_data);
free(res_data);
return 0;
}
|