Skip to content

Commit 017bcab

Browse files
committed
Code flow authentication for persistent session. Addresses #2
1 parent 548a563 commit 017bcab

File tree

2 files changed

+279
-15
lines changed

2 files changed

+279
-15
lines changed

OneDrive_Gaph_tutorial.ipynb

Lines changed: 155 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@
2121
"import requests\n",
2222
"import json\n",
2323
"import urllib\n",
24-
"import os"
24+
"import os\n",
25+
"from getpass import getpass\n",
26+
"import time\n",
27+
"from datetime import datetime"
2528
]
2629
},
2730
{
2831
"cell_type": "markdown",
2932
"metadata": {},
3033
"source": [
31-
"### Get Access token"
34+
"## Get Access token\n",
35+
"\n",
36+
"### Token flow authentication"
3237
]
3338
},
3439
{
@@ -85,7 +90,154 @@
8590
"cell_type": "markdown",
8691
"metadata": {},
8792
"source": [
88-
"Looks all right. We have got the access token, and included in the HEADERS. You can print response to see more. "
93+
"Looks all right. We have got the access token, and included in the HEADERS. You can print response to see more. Go ahead with OneDrive operations."
94+
]
95+
},
96+
{
97+
"source": [
98+
"### Code flow authentication\n",
99+
"\n",
100+
"Code flow returns both `access_token` and `refresh_token` which can be used to\n",
101+
"request new `access_token` and `refresh_token` for persistent session. If you \n",
102+
"are using organization account, you might require consent of organization administrator. \n"
103+
],
104+
"cell_type": "markdown",
105+
"metadata": {}
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": null,
110+
"metadata": {},
111+
"outputs": [],
112+
"source": [
113+
"# Get code\n",
114+
"URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'\n",
115+
"client_id = \"9b50276c-89f9-4dfd-99e8-ab75c9927aa8\"\n",
116+
"permissions = ['offline_access', 'files.readwrite', 'User.Read']\n",
117+
"response_type = 'code'\n",
118+
"redirect_uri = 'http://localhost:8080/'\n",
119+
"scope = ''\n",
120+
"for items in range(len(permissions)):\n",
121+
" scope = scope + permissions[items]\n",
122+
" if items < len(permissions)-1:\n",
123+
" scope = scope + '+'\n",
124+
"\n",
125+
"print('Click over this link ' +URL + '?client_id=' + client_id + '&scope=' + scope + '&response_type=' + response_type+\\\n",
126+
" '&redirect_uri=' + urllib.parse.quote(redirect_uri))\n",
127+
"print('Sign in to your account, copy the whole redirected URL.')\n",
128+
"code = getpass(\"Paste the URL here :\")\n",
129+
"code = code[(code.find('?code') + len('?code') + 1) :]\n",
130+
"\n",
131+
"URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/token'\n",
132+
"\n",
133+
"response = requests.post(URL + '?client_id=' + client_id + '&scope=' + scope + '&grant_type=authorization_code' +\\\n",
134+
" '&redirect_uri=' + urllib.parse.quote(redirect_uri)+ '&code=' + code)"
135+
]
136+
},
137+
{
138+
"cell_type": "code",
139+
"execution_count": null,
140+
"metadata": {},
141+
"outputs": [],
142+
"source": [
143+
"# Get token\n",
144+
"data = {\n",
145+
" \"client_id\": client_id,\n",
146+
" \"scope\": permissions,\n",
147+
" \"code\": code,\n",
148+
" \"redirect_uri\": redirect_uri,\n",
149+
" \"grant_type\": 'authorization_code',\n",
150+
" \"client_secret\": client_secret\n",
151+
"}\n",
152+
"\n",
153+
"response = requests.post(URL, data=data)\n",
154+
"\n",
155+
"token = json.loads(response.text)[\"access_token\"]\n",
156+
"refresh_token = json.loads(response.text)[\"refresh_token\"]"
157+
]
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": null,
162+
"metadata": {},
163+
"outputs": [],
164+
"source": [
165+
"# Refresh token\n",
166+
"def get_refresh_token():\n",
167+
" data = {\n",
168+
" \"client_id\": client_id,\n",
169+
" \"scope\": permissions,\n",
170+
" \"refresh_token\": refresh_token,\n",
171+
" \"redirect_uri\": redirect_uri,\n",
172+
" \"grant_type\": 'refresh_token',\n",
173+
" \"client_secret\": 'xxxx-yyyy-zzzz',\n",
174+
" }\n",
175+
"\n",
176+
" response = requests.post(URL, data=data)\n",
177+
"\n",
178+
" token = json.loads(response.text)[\"access_token\"]\n",
179+
" refresh_token = json.loads(response.text)[\"refresh_token\"]\n",
180+
" last_updated = time.mktime(datetime.today().timetuple())\n",
181+
"\n",
182+
" return token, refresh_token, last_updated"
183+
]
184+
},
185+
{
186+
"cell_type": "code",
187+
"execution_count": null,
188+
"metadata": {},
189+
"outputs": [],
190+
"source": [
191+
"token, refresh_token, last_updated = get_refresh_token()"
192+
]
193+
},
194+
{
195+
"source": [
196+
"If you have a large data to upload, you may use below mock code inside your upload loop:\n",
197+
"\n",
198+
"```python\n",
199+
"elapsed_time = time.mktime(datetime.today().timetuple()) - last_updated\n",
200+
"\n",
201+
"if (elapsed_time < 45*60*60):\n",
202+
" do_something()\n",
203+
"else if (elapsed_time < 59*60*60):\n",
204+
" token, refresh_token, last_updated = get_refresh_token()\n",
205+
"else:\n",
206+
" go_to_code_flow()\n",
207+
"```"
208+
],
209+
"cell_type": "markdown",
210+
"metadata": {}
211+
},
212+
{
213+
"source": [
214+
"## OneDrive operations"
215+
],
216+
"cell_type": "markdown",
217+
"metadata": {}
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": null,
222+
"metadata": {},
223+
"outputs": [],
224+
"source": [
225+
"URL = 'https://graph.microsoft.com/v1.0/'\n",
226+
"\n",
227+
"HEADERS = {'Authorization': 'Bearer ' + token}\n",
228+
"\n",
229+
"response = requests.get(URL + 'me/drive/', headers = HEADERS)\n",
230+
"if (response.status_code == 200):\n",
231+
" response = json.loads(response.text)\n",
232+
" print('Connected to the OneDrive of', response['owner']['user']['displayName']+' (',response['driveType']+' ).', \\\n",
233+
" '\\nConnection valid for one hour. Refresh token if required.')\n",
234+
"elif (response.status_code == 401):\n",
235+
" response = json.loads(response.text)\n",
236+
" print('API Error! : ', response['error']['code'],\\\n",
237+
" '\\nSee response for more details.')\n",
238+
"else:\n",
239+
" response = json.loads(response.text)\n",
240+
" print('Unknown error! See response for more details.')"
89241
]
90242
},
91243
{

0 commit comments

Comments
 (0)