0

I am trying access my CloudFoundry apps in python and i am using requests module.
First I have tried

import requests
response=requests.get("https://api.run.pivotal.io")

It is showing UnAuthorized error. for my knowledge First we have to get the token from authentication server. so I have used

response=requests.get('https://login.run.pivotal.io',auth=('username','pwd'))

It is successfull and getting response code 200. my doubt is how to get my apps data. I am trying

response=requests.get("https://api.run.pivotal.io/v2/apps",'Authorization':'access_token myToken')

but still getting UnAuthorized error. How to give access token in headers, because login url is different so that i am not able to access that.

2

2 Answers 2

3
import requests
oauthTokenResponse = requests.post(
    'https://login.run.pivotal.io/oauth/token?grant_type=password&client_id=cf', 
    data={'username': 'YOUR_USERNAME', 'password': 'YOUR_PASSWORD', 'client_id': 'cf'},
    auth=('cf', '')
)
authorization = oauthTokenResponse.json()['token_type'] + ' ' + oauthTokenResponse.json()['access_token']
appsResponse = requests.get(
    "https://api.run.pivotal.io/v2/apps",
    headers={'Accept': 'application/json', 'Content-Type': 'application/json', 'Authorization': authorization}
)
1
  • It's perfectly working, and I have another doubt using requests only I am trying to upload bits appsResponse = requests.put( "https://api.run.pivotal.io/v2/apps/914e28c6-d987-4917-97fd-55185688c314/bits", headers={'Accept': 'application/json', 'Content-Type': 'application/zip', 'Authorization': authorization}, data={'application':'application.zip'} ) But it's not working
    – kishore
    Jan 11, 2016 at 10:49
0

You can also try something else, you will need a CLI for this script https://github.com/cloudfoundry/cli

from __future__ import print_function
import subprocess
import os
import re
import getpass

User = raw_input("Please Enter your user name: \n").lower()
if not re.match("^[a-zA-Z, 0-99]*$", User):
    print ("Error! Only letters and numbers are allowed!")
    break
Pass = getpass.getpass(prompt= "Please enter your password: \n")


URL_login = 'cf login --skip-ssl-validation -a https://api.run.pivotal.io -u ' + User.lower() + ' -p ' + Pass
subprocess.call(URL_login)
os.system('cf apps > apps.csv')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.