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
}
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.