0

Alright, I need some assistance with what I think is an easy question, but after digging for quite a while I'll leave it to you brilliant people to show me why I'm not!

I'm attempting to access a provider api, with the URL I'm attempting to access being: .../proposals/AnotherTestProposal/city_furniture/items?filter=description$CONT"Ag" :: this is the way they want it passed, and it should return all items who's descriptions contain the string "Ag". I know I have two of those currently.

I'm using Django 1.9.4, and requests_oauthlib to create an OAuth1Session, I'm doing this successfully also, because I can access resources without URL params. The trouble is I can't get the "?filter=description..." part to encode properly and it's giving me a 401.

When I render the .contents to HTML I get:

{"status": 404, "message": "", "data": [], "arguments": {"id": "items?filter=description$CONT\"Ag\"", "collection": "city_furniture"}}

which is telling telling me that the "AG" part is being escaped, which I don't want. As in: I don't want \"Ag\", I want "Ag".

So, my question: how can I pass the URL, with params, so they are not containing the slashes as these are causing the URL to be invalid and therefore keeping me from accessing the data correctly?

Other, possibly irrelevant info:

  1. The params part of the URL string I'm passing to the OAuth1Session object now is: '/city_furniture/items%3Ffilter%3Ddescription%24CONT%22Ag%22'

  2. An example of filtering from the API's website: proposals/master/roads/items?filter=description$CONT"highway"

  3. I tried passing an 'encoding' arg to .get (in order to change the encoding used by to_native_string) but requests rejected it saying it was an invalid arg

Per comments, some additional code. Using a function name get_protected_code() to get the OAuth info passed correctly, then in views.py:

api_filter_url =  settings.IW_API_MODEL_COLLECTION + '/' + model_id + '/' + settings.IW_API_PROPOSAL_COLLECTION + '/' + proposal_name + '/city_furniture/items%3Ffilter%3Ddescription%24CONT%22Ag%22'
    json_model_info_pull = get_protected_data(api_filter_url)
    find_vendor = json_model_info_pull.content

def get_protected_data(url_str):
    ## ...stuffs to create OAuth1Session...
    adsk_pull = OAuth1Session(key,
                              client_secret=secret,
                              resource_owner_key=oauth_token_use,
                              resource_owner_secret=oauth_token_secret_use,
                              )
    headers = {'accept': 'application/vnd.autodesk.infraworks-v1+json'}
    api_url = settings.ADSK_IW_INFO_BASE_URL + url_str
    json_model_info_pull = adsk_pull.get(api_url, headers=headers)
    return json_model_info_pull
AL the X
  • 1,004
  • 11
  • 15
crobertsnc
  • 11
  • 2
  • The contents of your request are escaped properly, as the value of `id` is a strong literal, which cannot contain the same quotes used as delimiters unless escaped. Although it appears that Python is attempting to use the query parameters in that URL segment. Can you post the code that you're using to make the request (minus the consumer key and secret, of course)...? – AL the X Jun 15 '16 at 11:42
  • Submitted an edit for your approval. There's a better way to paste code on SO. – AL the X Jun 16 '16 at 11:45

1 Answers1

0

Looks like you're passing the parameters incorrectly by appending them to the end of the URL in URL-encoding, which requests is respecting as intentional, and the API endpoint is translating in an unintended manner.

From the documentation for requests, you should provide a params keyword argument to requests.get: a dict containing the key-value pairs that should be encoded and sent as the query string for the request. For example, to run a query against the GitHub API, we can pass an API token as a query parameter:

requests.get('http://api.github.com/user',
    params={ 'access_token' : MY_OAUTH_TOKEN })

The resultant request will contain a query string with an access_token parameter set to the value stored in MY_OAUTH_TOKEN and escaped properly, as needed. (Such tokens typically contain = characters, for example, that are invalid inside query string values.)

AL the X
  • 1,004
  • 11
  • 15
  • Still struggling with this - the params uses the same encoding so the same result (ending up with an escaped \"). I'll have to keep digging. – crobertsnc Jun 28 '16 at 16:28
  • Do you need to provide the quote to the endpoint, or is it expecting just the unquoted value? – AL the X Jun 28 '16 at 17:32