-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitly.py
More file actions
68 lines (36 loc) · 1.76 KB
/
Bitly.py
File metadata and controls
68 lines (36 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Current Implementation : Web FLow
At the authentication page, login with your bitly account and allow the web app. Copy the Redirect URL and paste it when the prompt asks. Then, enter your URL to be shortened.
There is an alternative way, I believe :
Access tokens can be directly exchanged for username and password in Bitly API, I have not used this approach in this script.
"""
from requests_oauthlib import OAuth2Session
import json
client_id = r"your_client_id"
client_secret = r"your_client_secret"
redirect_uri = "your_redirect_url"
oauth = OAuth2Session(client_id, redirect_uri=redirect_uri)
base_auth_url = "https://bitly.com/oauth/authorize"
token_url = "https://api-ssl.bitly.com/oauth/access_token"
authorization_url = oauth.authorization_url(base_auth_url)
print("Please authorise and continue - \n")
print(authorization_url[0])
redirect_response = input("\n Paste the redirect URL") #string
ind = redirect_response.find("&code=") + 6
code = redirect_response[ind:]
api_url_base = "https://api-ssl.bitly.com/oauth/access_token?client_id=+ client_id+ "&client_secret=+"+ client_secret +"&code="+code
token = oauth.fetch_token(api_url_base, authorization_response=redirect_response) #json
print(token)
long_url = input("Enter your URL: \n")
pydata = {
"group_guid": "your_group_guid", #get group guid by sending get request to the guid link mentioned in documentation
"domain": "bit.ly",
"long_url": long_url,
"title": "string",
}
data = json.dumps(pydata) # data is json now
headers = {'Authorization': 'Bearer {token}'}
r = oauth.post("https://api-ssl.bitly.com/v4/bitlinks", headers = headers, data=data)
final = json.loads(r.text)
print("Shortened URL : \n")
print(final['link'])