[SAoC] “D Language Client Libraries for Google APIs” project thread

Adam D. Ruppe destructionator at gmail.com
Wed Sep 9 18:44:58 UTC 2020


On Wednesday, 9 September 2020 at 18:35:22 UTC, Robert Aron wrote:
> Milestone 1 – Familiarize with dlang-requests, dub and OAuth 2.0


I've actually done much of this before so feel free to email if 
you ever need a pointer. I did it with my own http lib but it is 
easy enough to adapt.

refeshAccessToken()
         auto url = "https://www.googleapis.com/oauth2/v4/token";
         string[string] req = [
                 "client_id" : config.web.client_id.get!string,
                 "client_secret" : 
config.web.client_secret.get!string,
                 "refresh_token" : auth.refresh_token.get!string,
                 "grant_type" : "refresh_token"
         ];

         auto v = var.fromJson(cast(string) (post(url, 
req).waitForCompletion.content));


getCode()
         auto url = "https://accounts.google.com/o/oauth2/v2/auth";
         string[string] req = [
                 "client_id" : config.web.client_id.get!string,
                 "redirect_uri" : "http://localhost:9292/",
                 "scope" : [
                         "https://www.googleapis.com/auth/drive",
                         "https://spreadsheets.google.com/feeds/",
                         "https://www.googleapis.com/auth/surveys",
                         
"https://www.googleapis.com/auth/userinfo.email",
                 ].join(" "),
                 "access_type" : "offline",
                 "response_type" : "code"
         ];


consumeCode()

         string[string] req = [
                 "client_id" : config.web.client_id.get!string,
                 "client_secret" : 
config.web.client_secret.get!string,
                 "code" : code,
                 "redirect_uri" : "http://localhost:9292/",
                 "grant_type" : "authorization_code"
         ];

         auto creds_got = post(url, req);



update a google sheet

         var json = var.emptyObject;
         json.valueInputOption = "RAW"; // alternative is 
USER_ENTERED
         json.data = var.emptyArray;

         var obj = var.emptyObject;
         obj.range = "WHATEVERRRRR!B1:B4";
         obj.majorDimension = "COLUMNS"; // or could be ROWS to 
run left to right
         obj.values = [ ["1", "2", "3", "4"] ];

         json.data ~= obj;
         json.includeValuesInResponse = false;

         auto client = new HttpClient();
         auto request = 
client.request(arsd.http2.Uri("https://sheets.googleapis.com/v4/spreadsheets/SOMEID/values:batchUpdate"),
         HttpVerb.POST, cast(ubyte[]) json.toJson(), 
"application/json");
         request.requestParameters.headers ~= "Authorization: 
Bearer " ~ creds.access_token.get!string;




You get the idea, they aren't terribly complicated to do one 
piece at a time.


More information about the Digitalmars-d mailing list