OAuth2 Authorization

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
pk23
Posts: 110
Joined: 24 Apr 2015, 00:49

OAuth2 Authorization

Post by pk23 » 19 Oct 2021, 18:10

TLDR

Is there any script that can upload the local image (including Animated GIF) to Teknik (a image hoster similar to Imgur, that use OAuth2 Tokens API)?
OR, Is there any sample code that shows how to do OAuth2 Authorization in AHK?

Problem Background

I'm currently using the below script to upload images to Imgur and ImgBB. My process is:
  • For image/screenshot, I just copy it to the clipboard, then press Alt+J, this script gonna upload it to Imgur and fill the returned link into the clipboard. If press Alt+U, it will upload to ImgBB instead.
  • For animated GIF, I just select the .gif file in Explorer, then press Alt+L, script gonna upload it to Imgur and fill the returned link into the clipboard. If press Alt+U, upload to ImgBB instead.

Code: Select all

;The API_keys in code below have been changed to placeholders
#NoEnv
#SingleInstance FORCE
SetTitleMatchMode Regex
#Persistent
#NoTrayIcon
SendMode Input

#Include GDI+ standard library\Gdip_All.ahk
#Include CreateFormData.ahk
#Include JSON.ahk
#Include OSDTIP.ahk

;-------------------------------------------------------------------------------
;~ Function
;-------------------------------------------------------------------------------
{
    ;-------------------------------------------------------------------------------
    ;~ Base Part
    ;-------------------------------------------------------------------------------
    ;Save the image in clipboard to hard drive
    saveImagetoFile(pathwithoutSlash, ext := "png", open := false) {
        If !pToken := Gdip_Startup()
        {
            MsgBox, 48, gdiplus Error!, Gdiplus failed to start. Please ensure you have gdiplus on your system
            ExitApp
        }
        pBitmap:=Gdip_CreateBitmapFromClipboard()
        FilePath := pathwithoutSlash "." ext
        Gdip_SaveBitmapToFile(pBitmap, FilePath)
        Gdip_DisposeImage(pBitmap)
        Gdip_Shutdown(pToken)
        if ( open = true )
            Run, %FilePath%
        return FilePath
    }
    
    uploadsuccess() {
        OSDTIP_Alert("SUCCESS", "Upload Success", -3000, "V3")
    }
    
    ;-------------------------------------------------------------------------------
    ;~ ImgBB
    ;-------------------------------------------------------------------------------
    uploadimgbb(filepath) {
        IfExist % filepath
        {
            objParam := { "key": "164ef3a2ede7ea158b13984ce969995f"
                        , "image": [filepath] }
            CreateFormData(PostData, hdr_ContentType, objParam)
            whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
            whr.Open("POST", "https://api.imgbb.com/1/upload", true)
            whr.SetProxy(2,"localhost:4411")
            whr.SetRequestHeader("Content-Type", hdr_ContentType)
            whr.SetRequestHeader("Accept", "application/json")
            whr.Send(PostData)
            whr.WaitForResponse()
            result := whr.ResponseText
            if(result <> "") {
                resultJson := JSON.Load(result)
                if(resultJson.success == true) {
                    clipboard := resultJson.data.url	
                    uploadsuccess()
                }
                else msgbox Upload Failed!
            } else {
                msgbox Upload Failed!
            }
        }
    }
    
    ;-------------------------------------------------------------------------------
    ;~ imgur
    ;-------------------------------------------------------------------------------
    uploadimgur(filepath) {
        IfExist % filepath
        {
            objParam := { "type": "file"
                        , "album": "184MRYGLyJxEn0c"
                        , "image": [filepath] }
            CreateFormData(PostData, hdr_ContentType, objParam)
            whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
            whr.SetTimeouts(0, 60000, 30000, 120000)
            whr.Open("POST", "https://api.imgur.com/3/image", true)
            whr.SetProxy(2,"localhost:4411")
            whr.SetRequestHeader("Authorization", "Client-ID 6701edb7084e67c") 
            whr.SetRequestHeader("Content-Type", hdr_ContentType)
            whr.SetRequestHeader("Accept", "application/json")
            whr.Send(PostData)
            whr.WaitForResponse()
            result := whr.ResponseText
            if(result <> "") {
                resultJson := JSON.Load(result)
                if(resultJson.success == true) {
                    deletelink := "https://imgur.com/delete/" + resultJson.data.deletehash
                    link := resultJson.data.link
                    FileAppend, %link%`n, imgur_deletehashes.log, UTF-8
                    FileAppend, %deletelink%`n, imgur_deletehashes.log, UTF-8
                    FileAppend, -`n, imgur_deletehashes.log, UTF-8
                    clipboard := link	
                    uploadsuccess()
                }
                else {
                    FileAppend, %result%`n, imgur_error.log, UTF-8
                    msgbox Upload Failed!
                }
            } else {
                msgbox Upload Failed!
            }
        }
    }

}

;-------------------------------------------------------------------------------
;~ Main Part
;-------------------------------------------------------------------------------
{
    ;Upload the non-GIF image in clipboard to ImgBB
    !u::
        ;Save the image in clipboard to hard drive as temp file
        path := A_Temp "\" A_Now
        path := saveImagetoFile(path, "png")
        ;Upload this temp file
        uploadimgbb(path)
        return
    
    ;Upload the GIF in hard dive to ImgBB
    !o::
        Clipboard := 
        send, ^c
        clipwait
        uploadimgbb(Clipboard)
        return
        
    ;Upload the non-GIF image in clipboard to Imgur
    !j::
        path := A_Temp "\" A_Now
        path := saveImagetoFile(path, "png")
        uploadimgur(path)
        return
    
    ;Upload the GIF in hard dive to Imgur
    !l::
        Clipboard := 
        send, ^c
        clipwait
        uploadimgur(Clipboard)
        return

}
Question

I want this script to support uploading to Teknik as well, but unlike Imgur and ImgBB which use static invariant API_Key, Teknik uses OAuth2 Tokens (access tokens expire every once in a while and need to be re-requested). Check here for details:
https://help.teknik.io/API
https://github.com/ShareX/ShareX/issues/4533

I've never written OAuth2 Tokens related code before, and have only figured out its flow even just now.
Anyone good kind soul can give me some directions/advice/help (e.g. OAuth2 Authorization sample code written in AHK)?
Thanks.

nadure
Posts: 22
Joined: 26 Mar 2021, 23:02
Location: Korea
Contact:

Re: OAuth2 Authorization

Post by nadure » 20 Oct 2021, 16:28

viewtopic.php?f=6&t=89236

this library is the oauth2 authentication library for google.

but, i can't find the example for oauth2 authentication for Teknik in their docs. hm.. wher is it? :eh:


pk23
Posts: 110
Joined: 24 Apr 2015, 00:49

Re: OAuth2 Authorization

Post by pk23 » 21 Oct 2021, 15:16

Thanks nadure and malcev, after looking at the links you offered and also the code in the few results pages returned by google search “oauth2 site:https://www.autohotkey.com/boards”, I guess I better give up ...... looks not a easy task that can be solved in 2 hours lol

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: OAuth2 Authorization

Post by malcev » 21 Oct 2021, 15:20

You can look for algorithm here.
viewtopic.php?p=263499#p263499

pk23
Posts: 110
Joined: 24 Apr 2015, 00:49

Re: OAuth2 Authorization

Post by pk23 » 21 Oct 2021, 22:14

Yeah, saw that post earlier from google search results. It explains pretty clear! Thank you malcev.
Imgur was recently sold to a notorious company. After comparing more than a dozen image hosters^1, eventually I think Teknik is the best alternative. maybe it will only be worth the time after the day Imgur is really unusable ......
^1: imgbb imgbox ImageBam vgy.me PostImages.org cubeupload imgrock z4a.net abload.de imageshack photobucket imageban.ru pixhost upload.ee

Post Reply

Return to “Ask for Help (v1)”