如何使用PowerShell使用Bitly V4 API缩短URL?
我目前使用下面的PowerShell脚本通过Bitly V3 API缩短任何URL。我希望有人能帮助使用Bitly V4 API做同样的事情如何使用PowerShell使用Bitly V4 API缩短URL?,powershell,api,url-shortener,Powershell,Api,Url Shortener,我目前使用下面的PowerShell脚本通过Bitly V3 API缩短任何URL。我希望有人能帮助使用Bitly V4 API做同样的事情 函数新建短URL{ param( [参数(位置=0,必需=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)] $URL ) #https://app.bitly.com 美国石油学会 $OAuthToken=“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
函数新建短URL{
param(
[参数(位置=0,必需=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
$URL
)
#https://app.bitly.com 美国石油学会
$OAuthToken=“XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”
$MyURL=调用WebRequest-Urihttps://api-ssl.bitly.com/v3/shorten -Body@{access_token=$OAuthToken;longURL=$URL}-Method Get
$MyURLjson=$MyURL.Content |从json转换
$MyURLjson.data.url
}
通过阅读,它指出
以前版本的API使用查询参数提交令牌。这在v4.0中不再适用。令牌应该使用OAuth承载令牌规范,在请求中使用授权头
这意味着你不能这么做
$body = @{access_token=$OAuthToken;longURL=$URL}
相反,您必须将访问令牌放在请求的头中
$header = @{Authorization = "Bearer $OAuthToken"
而且。。。似乎是POST而不是GET
你的请求应该是这样的
$body = @{long_url= "https://stackoverflow.com/questions/60418169/how-to-shorten-url-with-bitly-v4-api-using-powershell?noredirect=1#comment106889121_60418169"} | convertto-json
$OAuthToken = "=========="
$header = @{Authorization="Bearer $OAuthToken"; Accept="application/json"; "Content-Type"="application/json"}
$MyURL=Invoke-WebRequest -Uri https://api-ssl.bitly.com/v4/shorten -Body $body -header $header -Method Post
$MyURLjson = $MyURL.Content | ConvertFrom-Json
$MyURLjson.link
你读过如何从v3迁移到v4吗?读过。我没能弄明白。我只是用V3中的API标记尝试了那个脚本。但是,它不会返回缩短的链接。上面写着“禁止”。我需要知道如何为此脚本生成OAuth2令牌。使用我提供的文档链接查看如何获取auth令牌。您使用相同的调用,但使用-credential开关检索身份验证令牌谢谢。我能够生成一个与您的脚本一起工作的通用API令牌。我担心每次生成OAuth2令牌时“不得不”使用API客户机密钥+客户机密钥。客户机密钥和密钥列为可选项。