cdeb Posted April 15, 2020 Posted April 15, 2020 Get generic access token Go to your Bitly, click on the hamburger menu on the top-right side > Settings > Advanced Settings > API Support > click on the link Generic Access Tokens. Type in your password and generate a generic token. That's what you'll use for authentication. Code In v4, you could use generic token as a bearer in your headers with each request like so: expandcollapse popup#include-once #include 'WinHttp.au3' #include <String.au3> Local $genericAccessToken = '<<YOUR_ACCESS_TOKEN>>' Local $long_url = '<<YOUR_LONG_URL>>' Local $short_url = bitly_get_shorten_URL_v4($genericAccessToken, $long_url, 1) If @error Then MsgBox($MB_SYSTEMMODAL, "", "bitly_get_shorten_URL_v4() - error: "&@error) ConsoleWrite("+++ $short_url:" & $short_url & @CRLF) ; #FUNCTION# ;=============================================================================== ; Name...........: bitly_get_shorten_URL_v4 ; Description ...: get shorten URL using Bitly v4 ; Syntax.........: bitly_get_shorten_URL_v4 ($genericAccessToken, $long_url [, $debug = 0 ]) ; Parameters ....: $genericAccessToken - string - access token ; $long_url - string - long url eg: https://www.autoitscript.com/forum/ ; $debug - [optional] Console debub ; Return values .: Success - Returns string short url ; Failure - Returns 0 and sets @error: ; |1 - _StringBetween failed ; Author ........: cdeb ; Related .......: winhttp ;============================================================================================ Func bitly_get_shorten_URL_v4($genericAccessToken, $long_url, $debug=0) Local $payload = '{"long_url": "' & $long_url & '"}' Local $apiUrl = 'https://api-ssl.bitly.com/' Local $sPath = "v4/bitlinks" Local $hOpen = _WinHttpOpen() If $debug Then ConsoleWrite(">>$hOpen:" & $hOpen & @CRLF) Local $hConnect = _WinHttpConnect($hOpen, $apiUrl) If $debug Then ConsoleWrite(">>$hConnect:" & $hConnect & @CRLF) Local $hRequest = _WinHttpOpenRequest($hConnect, "POST", $sPath, -1, -1, -1, $WINHTTP_FLAG_SECURE) _WinHttpAddRequestHeaders($hRequest, "Authorization: Bearer " & $genericAccessToken) Local $hSendRequest = _WinHttpSendRequest($hRequest, "Content-Type: application/json", $payload, StringLen($payload)) _WinHttpReceiveResponse($hRequest) Local $sHeader, $sResponse If _WinHttpQueryDataAvailable($hRequest) Then $sHeader = _WinHttpQueryHeaders($hRequest) If $debug Then ConsoleWrite(">>$sHeader:" & $sHeader & @CRLF) Do $sResponse &= _WinHttpReadData($hRequest) Until @error If $debug Then ConsoleWrite(">>>>>>>>>>> header:" & @CRLF & $sHeader & @CRLF & ">>Response:" & @CRLF & $sResponse & @CRLF) EndIf ; Close handles _WinHttpCloseHandle($hRequest) _WinHttpCloseHandle($hConnect) _WinHttpCloseHandle($hOpen) ; extract short link Local $aArray = _StringBetween($sResponse, '"link":"', '"') If UBound($aArray) == 1 Then Return(SetError(0, 0, $aArray[0])) Else Return(SetError(1, 0, 0)) EndIf EndFunc
Developers Jos Posted April 15, 2020 Developers Posted April 15, 2020 Is there a question or was this meant to be in the examples forum? Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now