pythondjangobox-apirauth

Box API access denied, permission issue when trying to create a folder


I don't know why it's not letting me create a file at Box. I tried to change the settings around, but I still can't find the solution. Here is the code inside my django's view file:

#from django.shortcuts import render
from django.http import HttpResponse
from rauth import OAuth2Service
import json

# Create your views here.
def access_box(request):
    CLIENT_ID = 'xxx'
    CLIENT_SECRET = 'xxx'
    box_storage = OAuth2Service(
                            name='Box',
                            client_id=CLIENT_ID,
                            client_secret=CLIENT_SECRET,
                            authorize_url='https://www.box.com/api/oauth2/authorize',
                            access_token_url='https://www.box.com/api/oauth2/token',
                            base_url='https://www.box.com/'
                            )
    redirect_uri = 'http://127.0.0.1:8000/access-box/'

    params = {          
          'redirect_uri': redirect_uri,
          'response_type': 'code',         
          }
    url = box_storage.get_authorize_url(**params)

    if request.GET:        
        if request.GET.get('code'):
            code = request.GET.get('code')   
            data = {'code': code,
                    'grant_type': 'authorization_code',
                    'redirect_uri': redirect_uri,
                    }     

            payload = {
                       'name': 'testfolder',
                       'id': '0',
                       }
            session = box_storage.get_auth_session(data=data, decoder=json.loads)
            r = session.post('https://api.box.com/2.0/folders', params=payload)
            print(r.url)
            print(r.json())
            #r = r.json()
            #html = "<html><body>request available! authentication code: {0}<p>{1}</p></body></html>".format(code, r)
            html = "<html><body>request available! authentication code: {0}</body></html>".format(code)    
            return HttpResponse(html)
        else:
            return HttpResponse("zilch!")
    else:
        html = "<html><body></h1>Allow rushdGYM access to Box.com</h1><p><a href='{0}'>Allow now</a></body></html>".format(url)    
        return HttpResponse(html)

This is the response I received trying to create a folder @ Box.

{u'status': 400, u'code': u'bad_request', u'request_id': u'2143252gdf3', u'context_info': {u'errors': [{u'reason': u'missing_parameter', u'message': u"'parent' is required", u'name': u'parent'}, {u'reason': u'missing_parameter', u'message': u"'name' is required", u'name': u'name'}]}, u'help_url': u'http://developers.box.com/docs/#errors', u'message': u'Bad Request', u'type': u'error'}

Any guidelines to steer me in the right direction?


Solution

  • First, look at the error message you posted; it clearly states what the errors are:

    [
        {u'reason': u'missing_parameter', u'message': u"'parent' is required", u'name': u'parent'}, 
        {u'reason': u'missing_parameter', u'message': u"'name' is required", u'name': u'name'}
    ]
    

    So, you'll have to supply parent and name in your request.


    Second, note that the Box API expects the payload to be JSON encoded and present in the body.

    As of now, you're sending it as a request (GET) parameter, using the params argument to session.post, you should use the data argument instead.

    Might also want to set your Content-Type header to application/json.