1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python3 bitlyでURL短縮

Posted at

GoogleのURL短縮が、2019年3月でサービス終了してしまうということで
bitlyのサービスを使用する。(無償版で1日1万件まで)

bitlyのTOKEN作成は以下参照


import json
import urllib.request, urllib.error

def GET_Url(access_token, longUrl):

    url = "https://api-ssl.bitly.com/v3/shorten"    #End Point
    values = {"access_token": access_token,"longUrl": longUrl} #引数

    data = urllib.parse.urlencode(values).encode('ascii')
    result = json.loads(urllib.request.urlopen(urllib.request.Request(url,data)).read())
    print(result)

    return result["data"]["url"]

if __name__ == "__main__":
  #bitlyのTOKEN 
    access_token = "c12d79XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    
  #Wiki バンダイナムコのページ
    longUrl="https://ja.wikipedia.org/wiki/%E3%83%90%E3%83%B3%E3%83%80%E3%82%A4%E3%83%8A%E3%83%A0%E3%82%B3%E3%82%A2%E3%83%9F%E3%83%A5%E3%83%BC%E3%82%BA%E3%83%A1%E3%83%B3%E3%83%88"
  #結果出力 
    print (GET_Url(access_token, longUrl))

■結果

http://bit.ly/2rqzWBW

■bitly TOKEN作成(https://app.bitly.com/)
1.ログイン後右上のセッティングボタン押下
1.png

2.「Setting」選択
2.png

3.「Generic Account Token」選択
3.png

4.「PASSWORD」欄にパスワードを入れ「GENERATE TOKEN」ボタン押下で生成
4.png

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?