python - Shorte.st Api python

标签 python api

我试图使用 Shorte.st api 使用我的 python 程序自动创建我的短链接,但我真的不知道如何使用 API! 在专用页面中只有这段代码:

curl H "public-api-token: ---" -X -d "urlToShorten=google.com" PUT http://api.shorte.st/v1/data/url {"status":"ok","shortenedUrl":"http:\/\/sh.st\/XXXX"}

在public-api-token中,我显然必须插入我的私有(private) token ,但由于curl适用于c(我认为),我如何将它们与python一起使用? 非常感谢

最佳答案

我更喜欢使用名为 requests ( http://docs.python-requests.org/en/latest/ ) 的 python 库来处理 http 请求。您所要做的就是发送一个您想要缩短为数据字典的网址,并在“public-api-token”键下的 header 中发送您的公共(public) api token 。您可以在 https://shorte.st/tools/api 上找到您的 api token 页。响应内容是json编码的字符串,需要解码才能得到dict对象。

import requests
response = requests.put("https://api.shorte.st/v1/data/url", {"urlToShorten":"google.com"}, headers={"public-api-token": "your_api_token"})
print response.content
>>> {"status":"ok","shortenedUrl":"http:\\/\\/sh.st\\/ryHyU"}
import json
decoded_response = json.loads(response.content)
print decoded_response
>>>{u'status': u'ok', u'shortenedUrl': u'http://sh.st/ryHyU'}

关于python - Shorte.st Api python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25958513/

相关文章:

python - Python 3.6+ 中的格式化字符串文字

python - Web3py send_raw_transaction ValueError : {'message' : 'invalid remainder' , 'code' : -32000}

python - 无法恢复类 TextVectorization 的图层 - 文本分类

amazon-web-services - 无法找到或加载主类 com.amazon.aes.webservices.client.cmd

javascript - 在 javascript 中投票

python - 元组 Python 中的斐波那契数列

wordpress - 在 WordPress 中使用 CloudConvert API

javascript - REST API azure 语音转文本(已识别 : Text=undefined)

api - 制作 REST API : How do I route a filepath using express for node

python - 如何从单个词典创建词典列表?