commit 6047211ea0da88d2254b0dc09e6a4a3409bb3cce
parent 9d8a71a4405cf9e7e3bae38dbac20c5f2d83b27d
Author: Andreas Gruhler <agruhl@gmx.ch>
Date: Mon, 20 Jun 2022 00:21:06 +0200
#3 read token file to mem
Diffstat:
1 file changed, 14 insertions(+), 22 deletions(-)
diff --git a/src/caldav.c b/src/caldav.c
@@ -43,11 +43,6 @@ static size_t curl_write_mem_callback(void * contents, size_t size, size_t nmemb
return realsize;
}
-static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream) {
- size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
- return written;
-}
-
// todo
// https://beej.us/guide/bgnet/html
void* get_in_addr(struct sockaddr *sa) {
@@ -191,29 +186,27 @@ void get_access_token(char* code, char* verifier, bool refresh) {
curl = curl_easy_init();
- FILE* tokenfile;
- char* tokenfile_path;
+ // https://curl.se/libcurl/c/getinmemory.html
+ struct curl_mem_chunk token_resp;
+ token_resp.memory = malloc(1);
+ if (token_resp.memory == NULL) {
+ perror("malloc failed");
+ return;
+ }
+ token_resp.size = 0;
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, GOOGLE_OAUTH_TOKEN_URL);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postfields);
- curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
- //curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
-
- tokenfile_path = expand_path(CONFIG.google_tokenfile);
- tokenfile = fopen(tokenfile_path, "wb");
- free(tokenfile_path);
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_write_mem_callback);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&token_resp);
- if (tokenfile == NULL) {
- perror("Warning - failed to open tokenfile in get_access_token()");
- return;
- }
- curl_easy_setopt(curl, CURLOPT_WRITEDATA, tokenfile);
res = curl_easy_perform(curl);
- fclose(tokenfile);
curl_easy_cleanup(curl);
+ fputs(token_resp.memory, stderr);
+
if (res != CURLE_OK) {
fprintf(stderr, "Error - curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
return;
@@ -223,7 +216,7 @@ void get_access_token(char* code, char* verifier, bool refresh) {
tokfile = read_tokenfile();
free(tokfile);
// Make sure the refresh token is re-written and persistet
- // to the tokenfile for further requests, because the
+ // to the tokenfile for further requests, because this token
// is not returned by the refresh_token call:
// https://developers.google.com/identity/protocols/oauth2/native-app#offline
write_tokenfile();
@@ -845,4 +838,4 @@ int caldav_sync(struct tm* date,
free(remote_uid);
free(home_set); // home_set was required for sprintfing calendar_href to event_uri
return conf_ch;
-}
-\ No newline at end of file
+}