Forum Stats

  • 3,768,625 Users
  • 2,252,822 Discussions
  • 7,874,652 Comments

Discussions

Python Scripts to Invoke Oracle Cloud REST API's

Praveen Gollu-Oracle
Praveen Gollu-Oracle Member Posts: 45 Employee
edited Jul 3, 2019 6:38AM in Python

Hi Experts,

I am new to Python. I am trying to invoke Oracle Cloud REST API's for integrations. Any experts who can through some light the below

1. Prerequisites for invoking REST API's (Library's to be Imported )

2. Authentication prerequisites

3. Sample scripts for invoking REST API's

Any information on this are highly appreciated.

Thanks,

Praveen Gollu

3929781

Best Answer

  • Dmytro Shurkov-Oracle
    Dmytro Shurkov-Oracle Member Posts: 2 Employee
    edited Jun 27, 2019 1:09PM Accepted Answer

    Hello,

    requests is enough for me

    You can go thru Basic authorization(auth=(user,pass)) or use something different like oauth2 etc. by putting of token into header.

    for instance:

    import requests

    response = requests.get(url, [headers=headers], [auth=(user,pass)], [params=params])

    response.status_code

    response.json()

Answers

  • Dmytro Shurkov-Oracle
    Dmytro Shurkov-Oracle Member Posts: 2 Employee
    edited Jun 27, 2019 1:09PM Accepted Answer

    Hello,

    requests is enough for me

    You can go thru Basic authorization(auth=(user,pass)) or use something different like oauth2 etc. by putting of token into header.

    for instance:

    import requests

    response = requests.get(url, [headers=headers], [auth=(user,pass)], [params=params])

    response.status_code

    response.json()

  • Praveen Gollu-Oracle
    Praveen Gollu-Oracle Member Posts: 45 Employee
    edited Jun 28, 2019 7:22AM

    Hi Dmytro,

    Thanks for the information. As suggested, I tried below logic. Facing the issue.

    -------------------------------------------------------------------------------------------------------------------------------------------

    import requests

    url = 'http://abcd-1234.xyz.oraclecloud.com/fscmRestApi/resources/11.13.18.05/projects' -- Dummy url in the post

    user = 'abc.xyz' -- Dummy username in the post

    password = 'Welcome' -- Dummy password in the post

    auth = [user,password]

    response = requests.get(url,auth)

    if response.status_code == 200:

        print('We are able to connect to REST API')

    else:

        print('we are not able to connect to REST API')

    -------------------------------------------------------------------------------------------------------------------------------------------

    I am facing below error. Any suggestions on the same are highly appreciated.

    Traceback (most recent call last):

      File "D:\Praveen\Projects\Python\PythonScripts\REST_API.py", line 7, in <module>

        response = requests.get(url,auth)
      File "D:\Praveen\Projects\Python\lib\site-packages\requests\api.py", line 75, in get
        return request('get', url, params=params, **kwargs)
      File "D:\Praveen\Projects\Python\lib\site-packages\requests\api.py", line 60, in request
        return session.request(method=method, url=url, **kwargs)
      File "D:\Praveen\Projects\Python\lib\site-packages\requests\sessions.py", line 519, in request
        prep = self.prepare_request(req)
      File "D:\Praveen\Projects\Python\lib\site-packages\requests\sessions.py", line 462, in prepare_request
        hooks=merge_hooks(request.hooks, self.hooks),
      File "D:\Praveen\Projects\Python\lib\site-packages\requests\models.py", line 313, in prepare
        self.prepare_url(url, params)
      File "D:\Praveen\Projects\Python\lib\site-packages\requests\models.py", line 431, in prepare_url
        enc_params = self._encode_params(params)
      File "D:\Praveen\Projects\Python\lib\site-packages\requests\models.py", line 97, in _encode_params
        for k, vs in to_key_val_list(data):
    ValueError: too many values to unpack (expected 2)
    [Finished in 0.796s]
  • Dmytro Shurkov-Oracle
    Dmytro Shurkov-Oracle Member Posts: 2 Employee
    edited Jun 28, 2019 8:04AM

    this line is incorrect:

    response = requests.get(url,auth)

    You need write it as:

    response = requests.get(url,auth=auth)

    url is first mandatory parameter, others  - non-mandatory (**kwargs) but you need to specify for what parameter(e.g. auth or headers) you are putting your variable (auth list).

    p.s. according to docs, auth should be tuple but you are using list. Just to keep things easier and avoid some issues, plz modify this part as well.

    If you behind the proxy, take a look here:

    https://2.python-requests.org/en/master/user/advanced/#id10

  • Praveen Gollu-Oracle
    Praveen Gollu-Oracle Member Posts: 45 Employee
    edited Jul 3, 2019 6:38AM

    Hi Experts,

    Thanks for your support and help. I am able to successfully invoke the REST API using Python script. Hope this will be helpful for the new developers to the python.

    Once again thanks for all your support.

    import requests
    import json

    url = '<SERVER URL>/fscmRestApi/resources/11.13.18.05/projects'
    user = 'USERNAME'
    password = 'PASSWORD'
    auth = [user,password]
    ##response = requests.get(url,auth = (user,password))
    response = requests.get(url, auth = (user,password))
    if response.status_code == 200:
        print('We are able to connect to GET REST API')
    else:
        print('we are not able to connect to GET REST API')

    data = response.text
    parsed=json.loads(data)
    for product in parsed['items']:
        project_name = product['ProjectName']
        project_description = product['ProjectDescription']
        project_id = str(product['ProjectId'])
        project_data = {'ProjectDescription': project_description + 'RESTTEST'}
        project_url = '<SERVERURL>/fscmRestApi/resources/11.13.18.05/projects' + '/' + project_id
        print(project_name)
        patch_response = requests.patch(url = project_url,auth=(user,password),json = {'ProjectDescription': project_description + 'RESTTEST'})
        if patch_response.status_code == 200:
            print('We are able to connect to PATCH REST API')
        else:
            print('we are not able to connect to PATCH REST API')

    3929781Tanzeel Mirza
  • User_8CHJX
    User_8CHJX Member Posts: 3

    I have a similar requirement. I need to download a file from UCM. How can we specify the filename on the request? Also when I call this by response=requests.get(url, auth=(user, password)) and try response.text, it gives me a javascript error,

    Note, my python IDE has been able to download response from other sources, with same requests library.