Google Oauth2 authenticating with service account

Post your working scripts, libraries and tools for AHK v1.1 and older
nadure
Posts: 22
Joined: 26 Mar 2021, 23:02
Location: Korea
Contact:

Google Oauth2 authenticating with service account

05 Apr 2021, 09:26

with clr library and c# code, get the access token for oauth2 authentication.

so dll libary and clr.ahk are needed.

file download link.
https blogattach.naver.com /5cc940f0e2b ... nt.dll.zip Broken Link for safety

* modified. te file is uploaded.
google_oauth2.service_account.dll.zip
(393.9 KiB) Downloaded 30 times

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#Include %A_ScriptDir%\lib\clr.ahk

; Example Scope
scopes := ["https www.googleapis.com /auth/spreadsheets"]  Broken Link for safety

; Use your key file.
filePath := "service.json"
Token := new GoogleAuthWithServiceAccount(filePath, scopes)
Return

class GoogleAuthWithServiceAccount
{
    __New(KeyfilePath, scopes)
    {
        this.filePath := KeyfilePath
        this.scopes := scopes
        this.check_validation()
        dll_path := A_ScriptDir . "\lib\google_api_with_service_token.dll"

        asm := CLR_LoadLibrary(dll_path)
        this.instance := asm.CreateInstance("google_api_with_service_token.google_api")
        
        arr := ComObjArray(8, scopes.MaxIndex()) ; make safe Array
        for k, v in this.scopes
        {
            arr[k-1] := v ; put the values
        }
        return this.instance.GetAccessTokenFromJSONKey(this.filePath, arr)

    }
    check_validation()
    {
        if(this.filePath = "")
        {
            throw Exception("There is no filePath.")
        }

        if !FileExist(this.filePath)
            throw Exception("There is no service token file.")
        
        SplitPath,% this.filePath, fileName, OutDir, Ext
        if (Ext != "json")
            throw Exception("Service Key's extension is maybe json."
            + " please check the valid filePath.")

        if(this.scopes.MaxIndex()=0)
        {
            if (this.scopes="")
                throw Exception("There is no scopes.")
        }

        if(StrLen(this.scopes) > 0)
            throw Exception("Scopes have to be array. Not string.")
        
        if(not this.scopes[1])
            throw Exception("Scope value is invalid. Please check that.")

    }
}
I was trying to get access token with pure autohotkey code.
But, there is no sutiable library for rsa encryption. So i have to make the new one. but that is very hard way. so i made dll using 3rd partition.

And below is the c# code in the dll.
No harmful.

Code: Select all

using Google.Apis.Auth.OAuth2;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace google_api_with_service_token
{
    class google_api
    {
        public static async Task<string> GetAccessTokenFromJSONKeyAsync(string jsonKeyFilePath, params string[] scopes)
        {
            using (var stream = new FileStream(jsonKeyFilePath, FileMode.Open, FileAccess.Read))
            {
                return await GoogleCredential
                    .FromStream(stream) // Loads key file
                    .CreateScoped(scopes) // Gathers scopes requested
                    .UnderlyingCredential // Gets the credentials
                    .GetAccessTokenForRequestAsync(); // Gets the Access Token
            }
        }

        public string GetAccessTokenFromJSONKey(string jsonKeyFilePath, params string[] scopes)
        {
            return GetAccessTokenFromJSONKeyAsync(jsonKeyFilePath, scopes).Result;
        }

        public void testStringMessage(string msg)
        {
            MessageBox.Show(msg);
        }

        public void testStringListMessage(string[] msgs)
        {
            MessageBox.Show(msgs[0]);
        }

    }
}

Last edited by nadure on 08 Apr 2021, 04:37, edited 1 time in total.
nadure
Posts: 22
Joined: 26 Mar 2021, 23:02
Location: Korea
Contact:

Re: Google Oauth2 authenticating with service account

12 Apr 2021, 06:43

malcev wrote:
11 Apr 2021, 17:56
I have written examples:
https://www.autohotkey.com/boards/viewtopic.php?f=6&t=89236
thx i've read the post.

that's an unbelievable code!

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 137 guests