Skip to content

Commit 548a563

Browse files
committed
Updated with move item example code
Resolves #1
1 parent a879eb3 commit 548a563

File tree

2 files changed

+78
-51
lines changed

2 files changed

+78
-51
lines changed

OneDrive_Gaph_tutorial.ipynb

+26-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"source": [
99
"# Program: Accessing OneDrive via Graph API\n",
1010
"# Author: Pranab Das (Twitter: @Pranab_Das)\n",
11-
"# Version: 20191104 "
11+
"# Version: 20210820"
1212
]
1313
},
1414
{
@@ -250,6 +250,30 @@
250250
" print(items[entries]['name'], '| item-id >', items[entries]['id'])"
251251
]
252252
},
253+
{
254+
"source": [
255+
"### Move itme"
256+
],
257+
"cell_type": "markdown",
258+
"metadata": {}
259+
},
260+
{
261+
"source": [
262+
"# url = URL + 'me/drive/items/{item-id-of-item-to-be-moved}'\n",
263+
"# provide item-id-of-destination-directory under parentReference in the body\n",
264+
"url = URL + 'me/drive/items/C1465DBECD7188C9!106'\n",
265+
"body = {\n",
266+
" \"parentReference\": {\n",
267+
" \"id\": \"C1465DBECD7188C9!103\"\n",
268+
" },\n",
269+
"}\n",
270+
"response = json.loads(requests.patch(url, headers=HEADERS, json=body).text)"
271+
],
272+
"cell_type": "code",
273+
"metadata": {},
274+
"execution_count": null,
275+
"outputs": []
276+
},
253277
{
254278
"cell_type": "markdown",
255279
"metadata": {},
@@ -553,4 +577,4 @@
553577
},
554578
"nbformat": 4,
555579
"nbformat_minor": 2
556-
}
580+
}

README.md

+52-49
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
# Access OneDrive via Graph API (Python code)
2-
Upload, download, rename your files and many more to your OneDrive both personal and business accounts using Microsoft Graph API (Python code).
2+
Upload, download, rename your files and many more to your OneDrive both personal
3+
and business accounts using Microsoft Graph API (Python code).
4+
5+
First you need to [register your app](Azure_app_signup_step_by_step.md) in the
6+
Azure portal.
37

48
```python
59
# Program: Accessing OneDrive via Graph API
610
# Author: Pranab Das (Twitter: @Pranab_Das)
7-
# Version: 20191104
11+
# Version: 20210820
812
```
913

10-
1114
```python
1215
# requirements
1316
import requests
@@ -16,27 +19,27 @@ import urllib
1619
import os
1720
```
1821

19-
### Get Access token
2022

23+
## Get Access token
2124

2225
```python
2326
URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
24-
client_id = "362422eb-d9d6-4245-9eca-2be5cf256450"
27+
client_id = "362422eb-d9d6-4245-9eca-2be5cf256450"
2528
permissions = ['files.readwrite']
2629
response_type = 'token'
27-
redirect_uri = 'http://localhost:8080/'
30+
redirect_uri = 'http://localhost:8080/'
2831
scope = ''
2932
for items in range(len(permissions)):
3033
scope = scope + permissions[items]
3134
if items < len(permissions)-1:
3235
scope = scope + '+'
33-
36+
3437
print('Click over this link ' +URL + '?client_id=' + client_id + '&scope=' + scope + '&response_type=' + response_type+\
3538
'&redirect_uri=' + urllib.parse.quote(redirect_uri))
3639
print('Sign in to your account, copy the whole redirected URL.')
3740
code = input("Paste the URL here :")
3841
token = code[(code.find('access_token') + len('access_token') + 1) : (code.find('&token_type'))]
39-
URL = 'https://graph.microsoft.com/v1.0/'
42+
URL = 'https://graph.microsoft.com/v1.0/'
4043
HEADERS = {'Authorization': 'Bearer ' + token}
4144
response = requests.get(URL + 'me/drive/', headers = HEADERS)
4245
if (response.status_code == 200):
@@ -47,25 +50,25 @@ elif (response.status_code == 401):
4750
response = json.loads(response.text)
4851
print('API Error! : ', response['error']['code'],\
4952
'\nSee response for more details.')
50-
else:
53+
else:
5154
response = json.loads(response.text)
5255
print('Unknown error! See response for more details.')
5356
```
5457

5558
Click over this link https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=362422eb-d9d6-4245-9eca-2be5cf256450&scope=files.readwrite&response_type=token&redirect_uri=http%3A//localhost%3A8080/
5659
Sign in to your account, copy the whole redirected URL.
5760
Paste the URL here :http://localhost:8080/#access_token=EwBYA8l6BAAUO9chh8cJscQLmU%2bLSWpbnr0vmwwAAXbgH8Q919pMC8ErHXfcrM/uuPv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QkEyNYqcVzo1CdsAg%3d%3d&token_type=bearer&expires_in=3600&scope=Files.ReadWrite%20Files.ReadWrite.All
58-
Connected to the OneDrive of ( personal ).
61+
Connected to the OneDrive of ( personal ).
5962
Connection valid for one hour. Reauthenticate if required.
6063

6164

62-
Looks all right. We have got the access token, and included in the HEADERS. You can print response to see more.
63-
64-
### List folders under root directory
65+
Looks all right. We have got the access token, and included in the HEADERS. You
66+
can print response to see more.
6567

66-
We will pring both directory names and item-d
6768

69+
## List folders under root directory
6870

71+
We will print both directory `name` and `item-d`
6972
```python
7073
items = json.loads(requests.get(URL + 'me/drive/root/children', headers=HEADERS).text)
7174
items = items['value']
@@ -78,8 +81,7 @@ for entries in range(len(items)):
7881
Getting started with OneDrive.pdf | item-id > C1465DBECD7188C9!102
7982

8083

81-
### Create new folder (in the root directory)
82-
84+
## Create new folder (in the root directory)
8385

8486
```python
8587
url = URL + 'me/drive/root/children/'
@@ -91,8 +93,7 @@ body = {
9193
response = json.loads(requests.post(url, headers=HEADERS, json=body).text)
9294
```
9395

94-
Now lets list the directory again
95-
96+
Now lets list the directory again:
9697

9798
```python
9899
items = json.loads(requests.get(URL + 'me/drive/root/children', headers=HEADERS).text)
@@ -109,8 +110,10 @@ for entries in range(len(items)):
109110

110111
Here we go, we have successfully created the folder New_Folder.
111112

112-
#### List folders under a sub-folder (need to use item-id)
113-
Note that if you need to create or list sub-folders, you need to use the item-id. The path/folder notation does not work everywhere.
113+
114+
## List folders under a sub-folder (need to use item-id)
115+
Note that if you need to create or list sub-folders, you need to use the
116+
`item-id`. The `path/folder` notation does not work everywhere.
114117

115118

116119
```python
@@ -121,8 +124,7 @@ for entries in range(len(items)):
121124
print(items[entries]['name'], '| item-id >', items[entries]['id'])
122125
```
123126

124-
Well there are no files or folders under the New_Folder. Ok let's create one.
125-
127+
Well there are no files or folders under the `New_Folder`. Ok let's create one.
126128

127129
```python
128130
url = URL + 'me/drive/items/C1465DBECD7188C9!106/children/'
@@ -135,8 +137,7 @@ data = {
135137
response = json.loads(requests.post(url, headers=HEADERS, json = data).text)
136138
```
137139

138-
Now let's print the list again.
139-
140+
Now let's print the list again.
140141

141142
```python
142143
url = URL + 'me/drive/items/C1465DBECD7188C9!106/children'
@@ -149,8 +150,7 @@ for entries in range(len(items)):
149150
sub_folder | item-id > C1465DBECD7188C9!107
150151

151152

152-
### Rename an item
153-
153+
## Rename an item
154154

155155
```python
156156
url = URL + 'me/drive/items/C1465DBECD7188C9!106'
@@ -160,7 +160,6 @@ body = {
160160
response = json.loads(requests.patch(url, headers=HEADERS, json = body).text)
161161
```
162162

163-
164163
```python
165164
url = URL + 'me/drive/items/root/children'
166165
items = json.loads(requests.get(url, headers=HEADERS).text)
@@ -175,26 +174,37 @@ for entries in range(len(items)):
175174
Getting started with OneDrive.pdf | item-id > C1465DBECD7188C9!102
176175

177176

178-
#### Delete item
177+
## Move item
178+
```python
179+
# url = URL + 'me/drive/items/{item-id-of-item-to-be-moved}'
180+
# provide item-id-of-destination-directory under parentReference in the body
181+
url = URL + 'me/drive/items/C1465DBECD7188C9!106'
182+
body = {
183+
"parentReference": {
184+
"id": "C1465DBECD7188C9!103"
185+
},
186+
}
187+
response = json.loads(requests.patch(url, headers=HEADERS, json=body).text)
188+
```
189+
179190

191+
## Delete item
180192

181193
```python
182194
url = '/me/drive/items/C1465DBECD7188C9!106'
183195
url = URL + url
184196
confirmation = input('Are you sure to delete the Item? (Y/n):')
185-
if (confirmation.lower()=='y'):
197+
if (confirmation.lower()=='y'):
186198
response = requests.delete(url, headers=HEADERS)
187199
if (response.status_code == 204):
188200
print('Item gone! If need to recover, please check OneDrive Recycle Bin.')
189-
else:
201+
else:
190202
print("Item not deleted.")
191203
```
192204

193205
Are you sure to delete the Item? (Y/n):y
194206
Item gone! If need to recover, please check OneDrive Recycle Bin.
195207

196-
197-
198208
```python
199209
url = URL + 'me/drive/items/root/children'
200210
items = json.loads(requests.get(url, headers=HEADERS).text)
@@ -208,8 +218,7 @@ for entries in range(len(items)):
208218
Getting started with OneDrive.pdf | item-id > C1465DBECD7188C9!102
209219

210220

211-
#### Find item-id by item name
212-
221+
## Find item-id by item name
213222

214223
```python
215224
items = json.loads(requests.get(URL + 'me/drive/items/root/children', headers=HEADERS).text)
@@ -228,8 +237,7 @@ if(item_id==''):
228237
Item-id of Documents : C1465DBECD7188C9!103
229238

230239

231-
#### Upload file
232-
240+
## Upload file
233241

234242
```python
235243
url = 'me/drive/root:/example_spectrum.txt:/content'
@@ -253,19 +261,20 @@ for entries in range(len(items)):
253261
Getting started with OneDrive.pdf | item-id > C1465DBECD7188C9!102
254262

255263

256-
### Access/Download data
257-
264+
## Access/Download data
258265

259266
```python
260267
url = 'me/drive/root:/example_spectrum.txt:/content'
261268
url = URL + url
262269
data = requests.get(url, headers=HEADERS).text
263270
```
264271

265-
You may like to save the data in a file in your local drive.
272+
You may like to save the data in a file in your local drive.
266273

267-
#### Upload large files (Can be used to upload small files as well)
268-
If you have files (probably larger than 4 MB), you need to create upload sessions.
274+
275+
## Upload large files (Can be used to upload small files as well)
276+
If you have files (probably larger than 4 MB), you need to create upload
277+
sessions.
269278

270279

271280
```python
@@ -278,7 +287,7 @@ file_size = os.path.getsize(file_path)
278287
chunk_size = 320*1024*10 # Has to be multiple of 320 kb
279288
no_of_uploads = file_size//chunk_size
280289
content_range_start = 0
281-
if file_size < chunk_size :
290+
if file_size < chunk_size :
282291
content_range_end = file_size
283292
else :
284293
content_range_end = chunk_size - 1
@@ -300,8 +309,8 @@ data.close()
300309
response2 = requests.delete(url)
301310
```
302311

303-
#### OneDrive storage usage
304312

313+
## OneDrive storage usage
305314

306315
```python
307316
response = json.loads(requests.get(URL + 'me/drive/', headers = HEADERS).text)
@@ -311,9 +320,3 @@ print('Using', used, 'GB (', round(used*100/total, 2),'%) of total', total, 'GB.
311320
```
312321

313322
Using 0.48 GB ( 9.6 %) of total 5.0 GB.
314-
315-
316-
317-
```python
318-
319-
```

0 commit comments

Comments
 (0)