Нашел код в интернете для интеграции c bitly
Сслыка на источник
Возникает ошибка при работе
Рабочий ли код?
Сслыка на источник
Возникает ошибка при работе
Рабочий ли код?
Код:
#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