|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "The Python SDK has the capability to operate on files and folders shared with a user via shared link.\n", |
| 8 | + "\n", |
| 9 | + "Simply use `client.with_shared_link()` to get a client instance that can access the shared resource." |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "code", |
| 14 | + "execution_count": 1, |
| 15 | + "metadata": { |
| 16 | + "collapsed": true |
| 17 | + }, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "from boxsdk import Client, OAuth2\n", |
| 21 | + "# Create an SDK client using a developer token\n", |
| 22 | + "client = Client(OAuth2(None, None, access_token='KoquazbCU7fxrMM0wSgGiAlWKZNeQGgq'))" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 2, |
| 28 | + "metadata": { |
| 29 | + "collapsed": true |
| 30 | + }, |
| 31 | + "outputs": [], |
| 32 | + "source": [ |
| 33 | + "# This is a shared link created with a different Box account\n", |
| 34 | + "SHARED_LINK_URL = 'https://app.box.com/s/gjkefibtegdn318xqnsu3koc35qt18z3'" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 4, |
| 40 | + "metadata": { |
| 41 | + "collapsed": false |
| 42 | + }, |
| 43 | + "outputs": [ |
| 44 | + { |
| 45 | + "data": { |
| 46 | + "text/plain": [ |
| 47 | + "u'Book1.xls'" |
| 48 | + ] |
| 49 | + }, |
| 50 | + "execution_count": 4, |
| 51 | + "metadata": {}, |
| 52 | + "output_type": "execute_result" |
| 53 | + } |
| 54 | + ], |
| 55 | + "source": [ |
| 56 | + "shared_item = client.get_shared_item(SHARED_LINK_URL)\n", |
| 57 | + "shared_item.name" |
| 58 | + ] |
| 59 | + }, |
| 60 | + { |
| 61 | + "cell_type": "code", |
| 62 | + "execution_count": 6, |
| 63 | + "metadata": { |
| 64 | + "collapsed": false |
| 65 | + }, |
| 66 | + "outputs": [ |
| 67 | + { |
| 68 | + "ename": "BoxAPIException", |
| 69 | + "evalue": "\nMessage: Not Found\nStatus: 404\nCode: not_found\nRequest id: 135510722156675a3fa6c6c\nHeaders: {'Content-Length': '242', 'Content-Encoding': 'gzip', 'Age': '0', 'Vary': 'Accept-Encoding', 'Server': 'ATS', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache, no-store', 'Date': 'Tue, 08 Dec 2015 22:31:27 GMT', 'Content-Type': 'application/json'}\nURL: https://api.box.com/2.0/files/39001292145\nMethod: GET\nContext info: {u'errors': [{u'reason': u'invalid_parameter', u'message': u\"Invalid value 'f_39001292145'. 'item' with value 'f_39001292145' not found\", u'name': u'item'}]}", |
| 70 | + "output_type": "error", |
| 71 | + "traceback": [ |
| 72 | + "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", |
| 73 | + "\u001b[0;31mBoxAPIException\u001b[0m Traceback (most recent call last)", |
| 74 | + "\u001b[0;32m<ipython-input-6-8b1c12239ca7>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# This account doesn't actually have permissions on this file outside of the shared link context\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mclient\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfile\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mshared_item\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", |
| 75 | + "\u001b[0;32m/Users/jmeadows/.virtualenvs/sdknotebooks/lib/python2.7/site-packages/boxsdk/object/item.pyc\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, fields, etag)\u001b[0m\n\u001b[1;32m 124\u001b[0m \u001b[0;31m# pylint:disable=arguments-differ\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 125\u001b[0m \u001b[0mheaders\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'If-None-Match'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0metag\u001b[0m\u001b[0;34m}\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0metag\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mNone\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 126\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mItem\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfields\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfields\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mheaders\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 127\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 128\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparent_folder\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 76 | + "\u001b[0;32m/Users/jmeadows/.virtualenvs/sdknotebooks/lib/python2.7/site-packages/boxsdk/object/base_object.pyc\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, fields, headers)\u001b[0m\n\u001b[1;32m 103\u001b[0m \u001b[0murl\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_url\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 104\u001b[0m \u001b[0mparams\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m'fields'\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m','\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfields\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m}\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mfields\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 105\u001b[0;31m \u001b[0mbox_response\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_session\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mparams\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mparams\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mheaders\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mheaders\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 106\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__class__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_session\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_object_id\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbox_response\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjson\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 107\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", |
| 77 | + "\u001b[0;32m/Users/jmeadows/.virtualenvs/sdknotebooks/lib/python2.7/site-packages/boxsdk/session/box_session.pyc\u001b[0m in \u001b[0;36mget\u001b[0;34m(self, url, **kwargs)\u001b[0m\n\u001b[1;32m 370\u001b[0m \u001b[0;34m`\u001b[0m\u001b[0municode\u001b[0m\u001b[0;34m`\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 371\u001b[0m \"\"\"\n\u001b[0;32m--> 372\u001b[0;31m \u001b[0mresponse\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_prepare_and_send_request\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'GET'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 373\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mBoxResponse\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresponse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 374\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", |
| 78 | + "\u001b[0;32m/Users/jmeadows/.virtualenvs/sdknotebooks/lib/python2.7/site-packages/boxsdk/session/box_session.pyc\u001b[0m in \u001b[0;36m_prepare_and_send_request\u001b[0;34m(self, method, url, headers, auto_session_renewal, expect_json_response, attempt_number, **kwargs)\u001b[0m\n\u001b[1;32m 274\u001b[0m \u001b[0mattempt_number\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 275\u001b[0m \u001b[0mfile_stream_positions\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfile_stream_positions\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 276\u001b[0;31m \u001b[0;34m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 277\u001b[0m )\n\u001b[1;32m 278\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", |
| 79 | + "\u001b[0;32m/Users/jmeadows/.virtualenvs/sdknotebooks/lib/python2.7/site-packages/boxsdk/session/box_session.pyc\u001b[0m in \u001b[0;36m_make_request\u001b[0;34m(self, method, url, headers, auto_session_renewal, expect_json_response, attempt_number, **kwargs)\u001b[0m\n\u001b[1;32m 358\u001b[0m )\n\u001b[1;32m 359\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 360\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_raise_on_unsuccessful_request\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnetwork_response\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexpect_json_response\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 361\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 362\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mnetwork_response\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 80 | + "\u001b[0;32m/Users/jmeadows/.virtualenvs/sdknotebooks/lib/python2.7/site-packages/boxsdk/session/box_session.pyc\u001b[0m in \u001b[0;36m_raise_on_unsuccessful_request\u001b[0;34m(self, network_response, expect_json_response, method, url)\u001b[0m\n\u001b[1;32m 213\u001b[0m \u001b[0murl\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0murl\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 214\u001b[0m \u001b[0mmethod\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mmethod\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 215\u001b[0;31m \u001b[0mcontext_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mresponse_json\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'context_info'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 216\u001b[0m )\n\u001b[1;32m 217\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mexpect_json_response\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_is_json_response\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnetwork_response\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", |
| 81 | + "\u001b[0;31mBoxAPIException\u001b[0m: \nMessage: Not Found\nStatus: 404\nCode: not_found\nRequest id: 135510722156675a3fa6c6c\nHeaders: {'Content-Length': '242', 'Content-Encoding': 'gzip', 'Age': '0', 'Vary': 'Accept-Encoding', 'Server': 'ATS', 'Connection': 'keep-alive', 'Cache-Control': 'no-cache, no-store', 'Date': 'Tue, 08 Dec 2015 22:31:27 GMT', 'Content-Type': 'application/json'}\nURL: https://api.box.com/2.0/files/39001292145\nMethod: GET\nContext info: {u'errors': [{u'reason': u'invalid_parameter', u'message': u\"Invalid value 'f_39001292145'. 'item' with value 'f_39001292145' not found\", u'name': u'item'}]}" |
| 82 | + ] |
| 83 | + } |
| 84 | + ], |
| 85 | + "source": [ |
| 86 | + "# This account doesn't actually have permissions on this file outside of the shared link context\n", |
| 87 | + "client.file(shared_item.id).get()" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": 7, |
| 93 | + "metadata": { |
| 94 | + "collapsed": false |
| 95 | + }, |
| 96 | + "outputs": [ |
| 97 | + { |
| 98 | + "data": { |
| 99 | + "text/plain": [ |
| 100 | + "<boxsdk.object.file.File at 0x10712db90>" |
| 101 | + ] |
| 102 | + }, |
| 103 | + "execution_count": 7, |
| 104 | + "metadata": {}, |
| 105 | + "output_type": "execute_result" |
| 106 | + } |
| 107 | + ], |
| 108 | + "source": [ |
| 109 | + "# Operations on the item are allowed using client.get_shared_item() or client.with_shared_item()\n", |
| 110 | + "client.with_shared_link(SHARED_LINK_URL, None).file(shared_item.id).get()" |
| 111 | + ] |
| 112 | + } |
| 113 | + ], |
| 114 | + "metadata": { |
| 115 | + "kernelspec": { |
| 116 | + "display_name": "Python 2", |
| 117 | + "language": "python", |
| 118 | + "name": "python2" |
| 119 | + }, |
| 120 | + "language_info": { |
| 121 | + "codemirror_mode": { |
| 122 | + "name": "ipython", |
| 123 | + "version": 2 |
| 124 | + }, |
| 125 | + "file_extension": ".py", |
| 126 | + "mimetype": "text/x-python", |
| 127 | + "name": "python", |
| 128 | + "nbconvert_exporter": "python", |
| 129 | + "pygments_lexer": "ipython2", |
| 130 | + "version": "2.7.9" |
| 131 | + } |
| 132 | + }, |
| 133 | + "nbformat": 4, |
| 134 | + "nbformat_minor": 0 |
| 135 | +} |
0 commit comments