Skip to content

Commit

Permalink
Updated with move item example code
Browse files Browse the repository at this point in the history
Resolves #1
  • Loading branch information
pranabdas committed Aug 22, 2021
1 parent a879eb3 commit 548a563
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 51 deletions.
28 changes: 26 additions & 2 deletions OneDrive_Gaph_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"source": [
"# Program: Accessing OneDrive via Graph API\n",
"# Author: Pranab Das (Twitter: @Pranab_Das)\n",
"# Version: 20191104 "
"# Version: 20210820"
]
},
{
Expand Down Expand Up @@ -250,6 +250,30 @@
" print(items[entries]['name'], '| item-id >', items[entries]['id'])"
]
},
{
"source": [
"### Move itme"
],
"cell_type": "markdown",
"metadata": {}
},
{
"source": [
"# url = URL + 'me/drive/items/{item-id-of-item-to-be-moved}'\n",
"# provide item-id-of-destination-directory under parentReference in the body\n",
"url = URL + 'me/drive/items/C1465DBECD7188C9!106'\n",
"body = {\n",
" \"parentReference\": {\n",
" \"id\": \"C1465DBECD7188C9!103\"\n",
" },\n",
"}\n",
"response = json.loads(requests.patch(url, headers=HEADERS, json=body).text)"
],
"cell_type": "code",
"metadata": {},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -553,4 +577,4 @@
},
"nbformat": 4,
"nbformat_minor": 2
}
}
101 changes: 52 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# Access OneDrive via Graph API (Python code)
Upload, download, rename your files and many more to your OneDrive both personal and business accounts using Microsoft Graph API (Python code).
Upload, download, rename your files and many more to your OneDrive both personal
and business accounts using Microsoft Graph API (Python code).

First you need to [register your app](Azure_app_signup_step_by_step.md) in the
Azure portal.

```python
# Program: Accessing OneDrive via Graph API
# Author: Pranab Das (Twitter: @Pranab_Das)
# Version: 20191104
# Version: 20210820
```


```python
# requirements
import requests
Expand All @@ -16,27 +19,27 @@ import urllib
import os
```

### Get Access token

## Get Access token

```python
URL = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize'
client_id = "362422eb-d9d6-4245-9eca-2be5cf256450"
client_id = "362422eb-d9d6-4245-9eca-2be5cf256450"
permissions = ['files.readwrite']
response_type = 'token'
redirect_uri = 'http://localhost:8080/'
redirect_uri = 'http://localhost:8080/'
scope = ''
for items in range(len(permissions)):
scope = scope + permissions[items]
if items < len(permissions)-1:
scope = scope + '+'

print('Click over this link ' +URL + '?client_id=' + client_id + '&scope=' + scope + '&response_type=' + response_type+\
'&redirect_uri=' + urllib.parse.quote(redirect_uri))
print('Sign in to your account, copy the whole redirected URL.')
code = input("Paste the URL here :")
token = code[(code.find('access_token') + len('access_token') + 1) : (code.find('&token_type'))]
URL = 'https://graph.microsoft.com/v1.0/'
URL = 'https://graph.microsoft.com/v1.0/'
HEADERS = {'Authorization': 'Bearer ' + token}
response = requests.get(URL + 'me/drive/', headers = HEADERS)
if (response.status_code == 200):
Expand All @@ -47,25 +50,25 @@ elif (response.status_code == 401):
response = json.loads(response.text)
print('API Error! : ', response['error']['code'],\
'\nSee response for more details.')
else:
else:
response = json.loads(response.text)
print('Unknown error! See response for more details.')
```

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/
Sign in to your account, copy the whole redirected URL.
Paste the URL here :http://localhost:8080/#access_token=EwBYA8l6BAAUO9chh8cJscQLmU%2bLSWpbnr0vmwwAAXbgH8Q919pMC8ErHXfcrM/uuPvmmsIyKar8nmAp1mvv/0QwrjAkSBM8Y6sJqpPEPrGKBrDHairoIVrQK7FhGCtYLGEy3P88wnaKGr4NYygckbi2g6P4S5KPt7d3m3/7XuAhLips6jwD3X8g89a72SajQaa1xbPFw2TfUed/UG6kqUxzlVUy4gkPCBMm%2bizQ3mP7lXRbmeXqCY5omTSQz6djvkCcjXf9TqC1WfVpRLHGc7yLUPcg15nGmdMfwRxWDxYi8rlD34Y0cVYt4KYw3B2VkdxyMvCWqARgauWApLYTFopGZIUQ8M0Fggb89PncdhHInKehD8Rp7rkBJIhkfIIDZgAACBnA%2btK5eKnhKAJmVnI6%2b2MwF54q9NR04O9xTn0Py/uOJPpyGeAtMRBgHTSI6Eh/Bwr/ybQh2TMbfNBbqpOEjPYx0KDhDhrcS1LldJKKoYj2EOREEkwKZNKYfmTdO1jWQ/MohoOFawGB29gdSyJxkqgRHrC2RedL3wFYMOxE78ehVvfCl1/UqBR4Z4ypPMZ%2bsFlyCOCQ6E2fiLyJt0AF5wZencLGoAhXdlh/gIDVZuSZBVQXZuEP19d07IGqLmwDoVnhecniQMjy3cLVQ5v0vlT15b/GpuESNhtgrdQwGT307F9gHPVO6U9UMzfT1iEx%2bjqOBR5paJz8OiIZOG3SZmqZFB4c606Vycio3BVnkXyNlf6kBZfJMNVLB4IubmXSbM%2byFjadP1Cq3pc2dsQRx%2bMqhYCDYS%2bYm4yBqHW0r/XfLrs/QmiIgVtAneHyw99TYVFEO2sqM3MLPZS1W8Wm0cFvwvfxuDI4cDllhjkX5jPy0wSD35c9rDZ8gwWdpR1x6Xc/XaTsn1eQEb9CcsZxyyIeJ6SA9t2kysZ1udqbu4xqIuMt3QtIdYA3tDDIg9IPJtF7tuC48G5tjm7BlfOANxhfsg8USYtjovd3KC4Jl4w87OBeiGrRQgaoI4pEfZVgYPa4TuOYe6ZuYCEyNW8GYumvetzmkRkrMBwicAqJ5KXVco5Lird6gCbQSWFjBTfxtdzXFKCiEgcQSDAm88xTwN2LvBKsrbV17QkEyNYqcVzo1CdsAg%3d%3d&token_type=bearer&expires_in=3600&scope=Files.ReadWrite%20Files.ReadWrite.All
Connected to the OneDrive of ( personal ).
Connected to the OneDrive of ( personal ).
Connection valid for one hour. Reauthenticate if required.


Looks all right. We have got the access token, and included in the HEADERS. You can print response to see more.

### List folders under root directory
Looks all right. We have got the access token, and included in the HEADERS. You
can print response to see more.

We will pring both directory names and item-d

## List folders under root directory

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


### Create new folder (in the root directory)

## Create new folder (in the root directory)

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

Now lets list the directory again

Now lets list the directory again:

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

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

#### List folders under a sub-folder (need to use item-id)
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.

## List folders under a sub-folder (need to use item-id)
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.


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

Well there are no files or folders under the New_Folder. Ok let's create one.

Well there are no files or folders under the `New_Folder`. Ok let's create one.

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

Now let's print the list again.

Now let's print the list again.

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


### Rename an item

## Rename an item

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


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


#### Delete item
## Move item
```python
# url = URL + 'me/drive/items/{item-id-of-item-to-be-moved}'
# provide item-id-of-destination-directory under parentReference in the body
url = URL + 'me/drive/items/C1465DBECD7188C9!106'
body = {
"parentReference": {
"id": "C1465DBECD7188C9!103"
},
}
response = json.loads(requests.patch(url, headers=HEADERS, json=body).text)
```


## Delete item

```python
url = '/me/drive/items/C1465DBECD7188C9!106'
url = URL + url
confirmation = input('Are you sure to delete the Item? (Y/n):')
if (confirmation.lower()=='y'):
if (confirmation.lower()=='y'):
response = requests.delete(url, headers=HEADERS)
if (response.status_code == 204):
print('Item gone! If need to recover, please check OneDrive Recycle Bin.')
else:
else:
print("Item not deleted.")
```

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



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


#### Find item-id by item name

## Find item-id by item name

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


#### Upload file

## Upload file

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


### Access/Download data

## Access/Download data

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

You may like to save the data in a file in your local drive.
You may like to save the data in a file in your local drive.

#### Upload large files (Can be used to upload small files as well)
If you have files (probably larger than 4 MB), you need to create upload sessions.

## Upload large files (Can be used to upload small files as well)
If you have files (probably larger than 4 MB), you need to create upload
sessions.


```python
Expand All @@ -278,7 +287,7 @@ file_size = os.path.getsize(file_path)
chunk_size = 320*1024*10 # Has to be multiple of 320 kb
no_of_uploads = file_size//chunk_size
content_range_start = 0
if file_size < chunk_size :
if file_size < chunk_size :
content_range_end = file_size
else :
content_range_end = chunk_size - 1
Expand All @@ -300,8 +309,8 @@ data.close()
response2 = requests.delete(url)
```

#### OneDrive storage usage

## OneDrive storage usage

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

Using 0.48 GB ( 9.6 %) of total 5.0 GB.



```python

```

0 comments on commit 548a563

Please sign in to comment.