-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-sheets.cpp
1007 lines (918 loc) · 22 KB
/
google-sheets.cpp
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* google-sheets.cpp
*/
#include "google-sheets.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <json/json.h>
#include "utilstring.h"
#define GOOGLE_TOKEN_URL "https://www.googleapis.com/oauth2/v4/token"
#define API_SHEET "https://sheets.googleapis.com/v4/spreadsheets/"
enum CSVState
{
UnquotedField,
QuotedField,
QuotedQuote
};
const static unsigned char* b64 = (unsigned char *) "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
std::string base64url_encode
(
const unsigned char* data,
size_t data_len
)
{
std::stringstream r;
size_t rc = 0;
size_t modulusLen = data_len % 3;
size_t pad = ((modulusLen & 1) << 1) + ((modulusLen & 2) >> 1);
size_t result_len = 4 * (data_len + pad) / 3;
if (pad == 2)
result_len -= 2;
else
if (pad == 1)
result_len -= 1;
std::string result(result_len, '\0');
size_t byteNo;
for (byteNo = 0; byteNo + 3 <= data_len; byteNo += 3)
{
unsigned char BYTE0 = data[byteNo];
unsigned char BYTE1 = data[byteNo + 1];
unsigned char BYTE2 = data[byteNo + 2];
result[rc++] = b64[BYTE0 >> 2];
result[rc++] = b64[((0x3 & BYTE0) << 4) + (BYTE1 >> 4)];
result[rc++] = b64[((0x0f & BYTE1) << 2) + (BYTE2 >> 6)];
result[rc++] = b64[0x3f & BYTE2];
}
if (pad == 2)
{
result[rc++] = b64[data[byteNo] >> 2];
result[rc++] = b64[(0x3 & data[byteNo]) << 4];
}
else if (pad == 1)
{
result[rc++] = b64[data[byteNo] >> 2];
result[rc++] = b64[((0x3 & data[byteNo]) << 4) + (data[byteNo + 1] >> 4)];
result[rc++] = b64[(0x0f & data[byteNo + 1]) << 2];
}
return result;
}
std::string base64_url_encode
(
const std::string &value
)
{
return base64url_encode((unsigned char *) &value[0], value.size());
}
/**
* @brief parse SCV record
*/
static std::vector<std::string> readCSVRow
(
const std::string &row
)
{
CSVState state = UnquotedField;
std::vector<std::string> fields;
fields.push_back("");
size_t i = 0; // index of the current field
for (int r = 0; r < row.length(); r++)
{
char c = row.at(r);
switch (state) {
case UnquotedField:
switch (c) {
case ',': // end of field
fields.push_back(""); i++;
break;
case '"':
state = QuotedField;
break;
default:
fields[i].push_back(c);
break;
}
break;
case QuotedField:
switch (c) {
case '"':
state = QuotedQuote;
break;
default:
fields[i].push_back(c);
break;
}
break;
case QuotedQuote:
switch (c) {
case ',': // , after closing quote
fields.push_back("");
i++;
state = UnquotedField;
break;
case '"': // "" -> "
fields[i].push_back('"');
state = QuotedField;
break;
default: // end of quote
state = UnquotedField;
break;
}
break;
}
}
return fields;
}
/**
* @brief Read CSV file
* http://stackoverflow.com/questions/1120140/how-can-i-read-and-parse-csv-files-in-c
*/
static void readCSV
(
std::istream &in,
std::vector<std::vector<std::string> > &retval
)
{
std::string row;
while (true)
{
std::getline(in, row);
if (in.bad() || in.eof())
break;
retval.push_back(readCSVRow(row));
}
}
/**
* @brief CURL write callback
*/
static size_t write_string(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
/**
* @brief POST data, return received data in retval
* Return 0- success, otherwise error code. retval contains error description
*/
static int curl_post0
(
const std::string &url,
const std::string &data,
std::string &retval
)
{
CURL *curl = curl_easy_init();
if (!curl)
return CURLE_FAILED_INIT;
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retval);
res = curl_easy_perform(curl);
long http_code;
if (res != CURLE_OK)
{
retval = curl_easy_strerror(res);
http_code = - res;
}
else
{
// std::cerr << "POST 0 return: " << retval << std::endl;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
}
curl_easy_cleanup(curl);
return http_code;
}
/**
* @brief GET
*/
int curl_get
(
const std::string &token,
const std::string &url,
std::string &retval
)
{
CURL *curl = curl_easy_init();
if (!curl)
return CURLE_FAILED_INIT;
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, ("authorization: Bearer " + token).c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retval);
CURLcode res = curl_easy_perform(curl);
long http_code;
if (res != CURLE_OK)
{
retval = curl_easy_strerror(res);
http_code = res;
}
else
{
// std::cerr << "GET return: " << retval << std::endl;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
}
curl_slist_free_all(chunk);
curl_easy_cleanup(curl);
return http_code;
}
/**
* @brief PUT
*/
int curl_put
(
const std::string &token,
const std::string &url,
const std::string &data,
std::string &retval
)
{
CURL *curl = curl_easy_init();
if (!curl)
return - CURLE_FAILED_INIT;
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, ("authorization: Bearer " + token).c_str());
chunk = curl_slist_append(chunk, "Content-Type: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retval);
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
CURLcode res = curl_easy_perform(curl);
long http_code;
if (res != CURLE_OK)
{
retval = curl_easy_strerror(res);
http_code = - res;
}
else
{
// std::cerr << "PUT return: " << retval << std::endl;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
}
curl_slist_free_all(chunk);
curl_easy_cleanup(curl);
return http_code;
}
/**
* @brief POST
* @param token GWT
* @param url locator
* @param data data to POST
* @return negative number: CURL error code(invert sign to get CURL code), positive number: HTTP code
*/
int curl_post
(
const std::string &token,
const std::string &url,
const std::string &data,
std::string &retval
)
{
CURL *curl = curl_easy_init();
if (!curl)
return CURLE_FAILED_INIT;
struct curl_slist *chunk = NULL;
chunk = curl_slist_append(chunk, ("authorization: Bearer " + token).c_str());
chunk = curl_slist_append(chunk, "Content-Type: application/json");
// Print out request
// curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_string);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &retval);
CURLcode res = curl_easy_perform(curl);
long http_code;
if (res != CURLE_OK)
{
retval = curl_easy_strerror(res);
http_code = - res;
}
else
{
// std::cerr << "POST return: " << retval << std::endl;
curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
}
curl_slist_free_all(chunk);
curl_easy_cleanup(curl);
return http_code;
}
/**
* @brief JWT signing. If error occurred, retval contains error descrtiption
* @param data data to sign
* @param pemkey PEM private key
* @param retval return value
* @return 0- success
*/
int jws_sign
(
const std::string &data,
const std::string &pemkey,
std::string &retval
)
{
SHA256_CTX mctx;
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_Init(&mctx);
SHA256_Update(&mctx, data.c_str(), data.size());
SHA256_Final(hash, &mctx);
unsigned char *sig;
size_t slen;
EVP_PKEY *key;
EVP_PKEY_CTX *kctx;
BIO *pem = BIO_new_mem_buf((void *) pemkey.c_str(), (int) pemkey.length());
if (pem == NULL)
goto err;
key = PEM_read_bio_PrivateKey(pem, NULL, NULL, (void *) "");
kctx = EVP_PKEY_CTX_new(key, NULL);
if (!kctx)
goto err;
if (EVP_PKEY_sign_init(kctx) <= 0)
goto err;
if (EVP_PKEY_CTX_set_rsa_padding(kctx, RSA_PKCS1_PADDING) <= 0)
goto err;
if (EVP_PKEY_CTX_set_signature_md(kctx, EVP_sha256()) <= 0)
goto err;
// Determine buffer length
slen = 0;
if (EVP_PKEY_sign(kctx, NULL, &slen, hash, SHA256_DIGEST_LENGTH) <= 0)
goto err;
sig = (unsigned char *) OPENSSL_malloc(slen);
if (!sig)
goto err;
if (EVP_PKEY_sign(kctx, sig, &slen, hash, SHA256_DIGEST_LENGTH) <= 0)
goto err;
retval = std::string((char *) sig, (unsigned int) slen);
return 0;
err:
// Clean up
EVP_cleanup();
int err = ERR_peek_error();
const char *r = ERR_error_string(err, NULL);
if (r)
retval = std::string(r);
else
retval = "Error";
return err;
}
/**
* @brief Generate JWT- base64url encoded JWT header
* @param service_account accounts.google.com, https://accounts.google.com.
* @param subject_email email
* @param pemkey PEM private key
* @param scope https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/plus.me
* @param audience app's client IDs
* @param expires 3600
* @param retval JWT
*/
static int getJWT
(
const std::string &service_account,
const std::string &subject_email,
const std::string &pemkey,
const std::string &scope,
const std::string &audience,
int expires,
std::string &retval
)
{
Json::Value jwt_header;
Json::Value jwt_claim_set;
time_t t = time(NULL);
Json::FastWriter jfw;
// Create jwt header
jwt_header["alg"] = "RS256";
jwt_header["typ"] = "JWT";
// jwt claim set
if (!subject_email.empty())
jwt_claim_set["sub"] = subject_email;
jwt_claim_set["iss"] = service_account;
jwt_claim_set["scope"] = scope;
// intended target of the assertion for an access token
jwt_claim_set["aud"] = audience;
jwt_claim_set["iat"] = Json::UInt64(t);
jwt_claim_set["exp"] = Json::UInt64(t + expires);
// header base64url
// for header
std::string header = jfw.write(jwt_header);
std::string base64url_header = base64_url_encode(header);
// claim set
std::string claim_set= jfw.write(jwt_claim_set);
std::string base64url_claim_set = base64_url_encode(claim_set);
retval = base64url_header + "." + base64url_claim_set;
// for signature
std::string jwt_signature;
int c = jws_sign(retval, pemkey, jwt_signature);
if (c != 0)
{
retval = jwt_signature;
return c;
}
retval += "." + base64_url_encode(jwt_signature);
return 0;
}
/**
* @brief Send JWT to the
* @param service_account accounts.google.com, https://accounts.google.com.
* @param subject_email email
* @param pemkey PEM private key
* @param scope https://www.googleapis.com/auth/spreadsheets https://www.googleapis.com/auth/plus.me
* @param audience app's client IDs
* @param expires 3600
*/
int loadGoogleToken
(
const std::string &service_account,
const std::string &subject_email,
const std::string &pemkey,
const std::string &scope,
const std::string &audience,
int expires,
std::string &retval,
int verbosity
)
{
std::string jwt;
int r = getJWT(service_account, subject_email, pemkey, scope, audience, expires, jwt);
if (r != 0)
{
if (verbosity > 2) {
std::cerr << "Error " << r << " GWT" << std::endl;
}
retval = jwt;
return r;
}
std::string json;
std::string rq = "grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=" + jwt;
if (verbosity > 2) {
std::cerr << "POST " << GOOGLE_TOKEN_URL << " " << rq << std::endl;
}
int res = curl_post0(GOOGLE_TOKEN_URL, rq, json);
Json::Reader reader;
Json::Value v;
if (res != 200)
{
retval = curl_easy_strerror((CURLcode) res);
if (verbosity > 2) {
std::cerr << "Error code " << res << " "
<< retval << ", description: " << json << std::endl;
}
if (reader.parse(json, v))
{
retval = v.get("error_description", "UTF-8").asString();
}
else
{
retval = json;
r = -1;
}
return res;
}
if (reader.parse(json, v))
{
retval = v.get("access_token", "UTF-8").asString();
}
else
{
retval = "Parse error " + json;
r = -1;
}
return r;
}
/**
* @brief load Google service token bearer from rhe Google service JSON object
*/
bool readGoogleTokenJSON
(
const std::string &json,
std::string &ret_service_account,
std::string &ret_pemkey,
std::string &email
)
{
Json::Reader reader;
Json::Value v;
if (json.empty() || (!reader.parse(json, v)))
return false;
ret_service_account = v["client_id"].asString();
ret_pemkey = v["private_key"].asString();
email = v["client_email"].asString();
size_t start_pos = 0;
while((start_pos = ret_pemkey.find("\\n", start_pos)) != std::string::npos)
{
ret_pemkey.replace(start_pos, 2, "\n");
start_pos++;
}
return true;
}
/**
* @brief clear values
*/
void ValueRange::clear()
{
range = "";
values.clear();
}
/**
* @brief parse data to the values
*/
int ValueRange::parseJSON(const std::string &json)
{
Json::Reader reader;
Json::Value v;
clear();
if (!reader.parse(json, v))
{
return -1;
}
range = v["range"].asString();
major_dimension = v["majorDimension"].asString();
for (Json::Value::const_iterator row_it(v["values"].begin()); row_it != v["values"].end(); ++row_it)
{
std::vector<std::string> r;
for (Json::Value::const_iterator it_cell(row_it->begin()); it_cell != row_it->end(); ++it_cell)
{
std::string s = it_cell->asString();
r.push_back(s);
}
values.push_back(r);
}
return 0;
}
ValueRange::ValueRange()
: major_dimension("ROWS")
{
}
ValueRange::ValueRange
(
const std::string &rang,
std::istream &stream)
: range(rang), major_dimension("ROWS")
{
values.clear();
readCSV(stream, values);
}
int ValueRange::parseCSV(const std::string &csv)
{
values.clear();
std::stringstream stream(csv);
readCSV(stream, values);
return 0;
}
/**
* @brief debug print
*/
std::string ValueRange::toString() const
{
std::stringstream ss;
ss << range << " " << major_dimension << std::endl;
for (std::vector<std::vector<std::string> >::const_iterator itr(values.begin()); itr != values.end(); ++itr)
{
std::vector<std::string> row = *itr;
for (std::vector<std::string>::const_iterator itc(itr->begin()); itc != itr->end(); ++itc)
{
ss << *itc << " ";
}
ss << std::endl;
}
return ss.str();
}
/**
* @brief get JSON string
*/
std::string ValueRange::toJSON() const
{
Json::Value root;
root["range"] = range;
root["majorDimension"] = major_dimension;
Json::Value rows = Json::Value(Json::arrayValue);
for (std::vector<std::vector<std::string> >::const_iterator itr(values.begin()); itr != values.end(); ++itr)
{
Json::Value cols = Json::Value(Json::arrayValue);
std::vector<std::string> row = *itr;
for (std::vector<std::string>::const_iterator itc(itr->begin()); itc != itr->end(); ++itc)
{
cols.append(*itc);
}
rows.append(cols);
}
root["values"] = rows;
Json::FastWriter jfw;
return jfw.write(root);
}
GoogleSheets::GoogleSheets
(
const std::string &spreadsheet,
const std::string &tokenbearer,
const std::string &service_account,
const std::string &subject_email,
const std::string &pemkey,
const std::string &scope,
const std::string &audience,
on_token_bearer onTokenbearer,
void *aenv,
int averbosity
)
: env(aenv), ontokenbearer(onTokenbearer), verbosity(averbosity)
{
sheet_id = spreadsheet;
token = tokenbearer;
genTokenParams.push_back(service_account);
genTokenParams.push_back(subject_email);
genTokenParams.push_back(pemkey);
genTokenParams.push_back(scope);
genTokenParams.push_back(audience);
if (verbosity > 2) {
std::cerr
<< "service_account: " << service_account << std::endl
<< "subject_email: " << subject_email << std::endl
<< "pemkey: " << pemkey << std::endl
<< "scope: " << scope << std::endl
<< "audience: " << audience << std::endl
<< std::endl;
}
if (token.empty()) {
if (verbosity > 2) {
std::cerr << "Token is empty " << std::endl;
}
genToken();
} else {
if (verbosity > 2) {
std::cerr << "Token: " << token << std::endl;
}
}
}
GoogleSheets::~GoogleSheets()
{
}
/**
* @brief Assign a new token received event handler
* Use to store token bearer
*/
void GoogleSheets::setOnTokenBearer
(
void *environ,
on_token_bearer value
)
{
env = environ;
ontokenbearer = value;
}
/**
* @brief Re-generate token
*/
bool GoogleSheets::genToken()
{
if (verbosity > 2) {
std::cerr << "Re-generate Google token.." << std::endl;
}
if (genTokenParams.size() < 5)
{
if (verbosity > 2) {
std::cerr << "Insufficient Google token parameters " << genTokenParams.size() << ", must have 5" << std::endl;
}
if (ontokenbearer)
ontokenbearer(env, token, -1);
return false;
}
if (verbosity > 2) {
std::cerr << "Loading Google token.." << std::endl;
}
int r = loadGoogleToken(
genTokenParams[0],
genTokenParams[1],
genTokenParams[2],
genTokenParams[3],
genTokenParams[4],
3600,
token,
verbosity
);
if (ontokenbearer)
ontokenbearer(env, token, r);
if (r != 0)
token = "";
return true;
}
/**
* @brief A1 notation
* @param column 0-> A
*/
static std::string columnFromNumber
(
int column
)
{
std::string columnString;
int columnNumber = column + 1;
while (columnNumber > 0)
{
int currentLetterNumber = (columnNumber - 1) % 26;
char currentLetter = (char)(currentLetterNumber + 65);
columnString = currentLetter + columnString;
columnNumber = (columnNumber - (currentLetterNumber + 1)) / 26;
}
return columnString;
}
/**
* @brief return A1 notation. column and row are zero based index
* @param column 0->A
* @param row 0->1
*/
std::string GoogleSheets::A1
(
const std::string &sheet,
int col,
int row
)
{
std::stringstream ss;
if (!sheet.empty())
ss << sheet << "!";
ss << columnFromNumber(col) << row + 1;
return ss.str();
}
std::string GoogleSheets::A1(
const std::string &sheet,
int col0,
int row0,
int col1,
int row1
)
{
std::stringstream ss;
if (!sheet.empty())
ss << sheet << "!";
ss << columnFromNumber(col0) << row0 + 1 << ":" << columnFromNumber(col1) << row1 + 1;
return ss.str();
}
/**
* @brief get values in range
*/
bool GoogleSheets::get
(
const std::string &sheet,
int col,
int row,
ValueRange &retval
)
{
return get(A1(sheet, col, row), retval);
}
/**
* @brief check is Google sheets request a new token bearer
* Error response e.g.
* {
* "error": {
* "code": 401,
* "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
* "status": "UNAUTHENTICATED"
* }
}
*/
int GoogleSheets::checkJSONErrorCode
(
const std::string &response
)
{
if (!response.empty())
{
// parse response
Json::Reader reader;
Json::Value v;
if (!reader.parse(response, v))
return 200;
if (v.isMember("error"))
{
Json::Value e = v["error"];
if (e["code"].isInt())
return e["error"].asInt();
}
}
return 200;
}
/**
* @brief GET spreadsheet range values. If token expires, regenerate token bearer and try again
*/
bool GoogleSheets::token_get(
const std::string &url,
ValueRange &retval
)
{
std::string response;
int r = curl_get(token, url, response);
if (verbosity > 2) {
std::cerr << "Token: " << token
<< ", url: " << url
<< ", response: " << response
<< ", code: " << r
<< std::endl;
}
if (checkJSONErrorCode(response) != 200)
{
if (verbosity > 2) {
std::cerr << "re-generate token.." << std::endl;
}
genToken();
r = curl_get(token, url, response);
}
bool ok = (r == 200);
if (ok)
retval.parseJSON(response);
else
retval.clear();
return ok;
}
/**
* @brief PUT spreadsheet range values. If token expires, regenerate token bearer and try again
*/
bool GoogleSheets::token_put(
const std::string &url,
const ValueRange &values
)
{
std::string json = values.toJSON();
std::string response;
int r = curl_put(token, url, json, response);
if (r != 200)
{
genToken();
r = curl_put(token, url, json, response);
}
return (r == 200);
}
/**
* @brief POST spreadsheet range values. If token expires, regenerate token bearer and try again
*/
bool GoogleSheets::token_post
(
const std::string &url,
const ValueRange &values
)
{
std::string json = values.toJSON();
std::string response;
int r = curl_post(token, url, json, response);
// std::cerr << json << std::endl;
if (r != 200)
{
genToken();
if (r != 401) {
std::cerr << r << ": " << response << std::endl;
}
r = curl_post(token, url, json, response);
}
return r == 200;
}
/**
* @brief get values in a range
* @param range Sheet1!A1:D5
*/
bool GoogleSheets::get
(
const std::string &range,
ValueRange &response
)
{
return token_get(API_SHEET + sheet_id + "/values/" + range, response);
}
/**
* @brief Replace values in a range
* @param values value range
*/
bool GoogleSheets::put
(
const ValueRange &values
)
{
return token_put(API_SHEET + sheet_id + "/values/" + values.range + "?valueInputOption=USER_ENTERED", values);
}
/**
* @brief Append values to a range
* @param values value range
*/
bool GoogleSheets::append