cURL Examples
cURL is a command line tool to make HTTP requests. It serves as a good tool for demonstrating the principal of the OAuth flow. Because it isn't a browser it can't do user authentication so is only useful for Client Credentials and Resource Owner Password Credentials.
Client Credentials
Running from command Line
// Save the Client Id & Secret, already joined and encoded. $ CLIENT_ID_SECRET= 'OVhPZTQxbEo2RElrSFQ2aHAxSWhPWVBYM05nYTpiazBhR0kzZzhXUVJoN3FHS1lwemVwakhmdThh' // Request an OAuth token $ curl -d "grant_type=client_credentials" -H "Authorization: Basic $CLIENT_ID_SECRET, Content-Type: application/x-www-form-urlencoded" https: //api-qa.ucsd.edu:8243/token --> { "token_type" : "Bearer" , "expires_in" : 3192 , "access_token" : "656d47cbf214c565c32286d856fe87d" } // Store the token $ TOKEN= '656d47cbf214c565c32286d856fe87d' // Invoke the API, including the token $ curl -s -H "Authorization :Bearer $TOKEN" 'https://api-qa.ucsd.edu:8243/weather/1.0.0/weather/?q=London,uk' --> { "coord" :{ "lon" :- 0.13 , "lat" : 51.51 }, "sys" :{ "message" : 0.0177 , "country" : "GB" , "sunrise" : 1428901694 , "sunset" : 1428951224 }, "weather" :[{ "id" : 804 , "main" : "Clouds" , "description" : "overcast clouds" , "icon" : "04n" }], "base" : "stations" , "main" :{ "temp" : 286.567 , "temp_min" : 286.567 , "temp_max" : 286.567 , "pressure" : 1034.42 , "sea_level" : 1042.45 , "grnd_level" : 1034.42 , "humidity" : 57 }, "wind" :{ "speed" : 3.27 , "deg" : 233.004 }, "clouds" :{ "all" : 92 }, "dt" : 1428959898 , "id" : 2643743 , "name" : "London" , "cod" : 200 } |
Bash Script
#!/usr/bin/env bash CLIENT_ID= '9XOe41lJ6DIkHT6hp1IhOYPX3Nga' CLIENT_SECRET= 'bk0aGI3g8WQRh7qGKYpzepjHfu8a' JSON=$(curl --user "$CLIENT_ID:$CLIENT_SECRET" -d "grant_type=client_credentials" https: //api-qa .ucsd.edu:8243 /token ) TOKEN=$( echo $JSON | sed 's/.*access_token":"\([^"]*\).*/\1/g' ) echo "Got access token $TOKEN" curl -H "Authorization :Bearer $TOKEN" 'https://api-qa.ucsd.edu:8243/weather/1.0.0/weather?q=London,uk' |