Display Multiple Form and Tree View For Same Model
Display Multiple Form and Tree View For Same Model
auth0.com
16-21 minutes
https://YOUR_DOMAIN/authorize?
audience=YOUR_API_AUDIENCE&
scope=YOUR_SCOPE&
response_type=code&
client_id=YOUR_CLIENT_ID&
redirect_uri=https://YOUR_APP/callback&
state=YOUR_OPAQUE_VALUE
Where:
audience: The unique identifier of the API the web app wants to
access. Use the Identifier value on the Settings tab for the API you
created as part of the prerequisites for this tutorial.
1 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
client_id: Your application's Client ID. You can find this value at
your Application's Settings.
For example:
<a href="https://YOUR_DOMAIN
/authorize?scope=appointments%20contacts&
audience=appointments:api&response_type=code&
client_id=YOUR_CLIENT_ID&redirect_uri=https:
//YOUR_APP/callback">
Sign In
</a>
The purpose of this call is to obtain consent from the user to invoke
the API (specified in audience) to do certain things (specified in
2 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
scope) on behalf of the user. Auth0 will authenticate the user and
obtain consent, unless consent has been previously given.
Note that if you alter the value in scope, Auth0 will require consent
to be given again.
Now that you have an Authorization Code, you must exchange it for
an Access Token that can be used to call your API. Using the
Authorization Code (code) from the previous step, you will need to
POST to the Token URL:
package main
3 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
import (
"fmt"
"strings"
"net/http"
"io/ioutil"
)
func main() {
url := "https://YOUR_DOMAIN/oauth/token"
payload :=
strings.NewReader("grant_type=authorization_code&
client_id=%24%7Baccount.clientId%7D&
client_secret=YOUR_CLIENT_SECRET&
code=YOUR_AUTHORIZATION_CODE&redirect_uri=
%24%7Baccount.callback%7D")
req.Header.Add("content-type",
"application/x-www-form-urlencoded")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
4 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
HttpResponse<String> response =
Unirest.post("https://YOUR_DOMAIN/oauth/token")
.header("content-type", "application/x-www-
form-urlencoded")
.body("grant_type=authorization_code&client_id=
%24%7Baccount.clientId%7D&
client_secret=YOUR_CLIENT_SECRET&
code=YOUR_AUTHORIZATION_CODE&redirect_uri=
%24%7Baccount.callback%7D")
.asString();
var options = {
method: 'POST',
url: 'https://YOUR_DOMAIN/oauth/token',
headers: {'content-type': 'application/x-www-
form-urlencoded'},
form: {
grant_type: 'authorization_code',
client_id: 'YOUR_CLIENT_ID',
client_secret: 'YOUR_CLIENT_SECRET',
code: 'YOUR_AUTHORIZATION_CODE',
redirect_uri: 'https://YOUR_APP/callback'
}
};
console.log(body);
});
#import <Foundation/Foundation.h>
5 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL
URLWithString:@"https://YOUR_DOMAIN/oauth/token"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"POST"];
[request setAllHTTPHeaderFields:headers];
[request setHTTPBody:postData];
6 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
if (error) {
NSLog(@"%@", error);
}
else {
NSHTTPURLResponse *httpResponse =
(NSHTTPURLResponse *) response;
NSLog(@"%@", httpResponse);
}
}];
[dataTask resume];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://YOUR_DOMAIN/oauth
/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>
"grant_type=authorization_code&client_id=
%24%7Baccount.clientId%7D&
7 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
client_secret=YOUR_CLIENT_SECRET&
code=YOUR_AUTHORIZATION_CODE&redirect_uri=
%24%7Baccount.callback%7D",
CURLOPT_HTTPHEADER => array(
"content-type: application/x-www-form-
urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
import http.client
conn = http.client.HTTPSConnection("")
payload = "grant_type=authorization_code&
client_id=%24%7Baccount.clientId%7D&
client_secret=YOUR_CLIENT_SECRET&
code=YOUR_AUTHORIZATION_CODE&redirect_uri=
%24%7Baccount.callback%7D"
conn.request("POST", "/YOUR_DOMAIN/oauth/token",
8 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
payload, headers)
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
require 'uri'
require 'net/http'
require 'openssl'
url = URI("https://YOUR_DOMAIN/oauth/token")
request = Net::HTTP::Post.new(url)
request["content-type"] = 'application/x-www-
form-urlencoded'
request.body = "grant_type=authorization_code&
client_id=%24%7Baccount.clientId%7D&
client_secret=YOUR_CLIENT_SECRET&
code=YOUR_AUTHORIZATION_CODE&redirect_uri=
%24%7Baccount.callback%7D"
response = http.request(request)
puts response.read_body
import Foundation
9 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
"grant_type=authorization_code".data(using:
String.Encoding.utf8)!)
postData.append("&client_id=YOUR_CLIENT_ID".data(using:
String.Encoding.utf8)!)
postData.append("&client_secret=YOUR_CLIENT_SECRET".data(using:
String.Encoding.utf8)!)
postData.append("&code=YOUR_AUTHORIZATION_CODE".data(using:
String.Encoding.utf8)!)
postData.append("&redirect_uri=https://YOUR_APP
/callback".data(using: String.Encoding.utf8)!)
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
10 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
})
dataTask.resume()
Where:
{
"access_token": "eyJz93a...k4laUWw",
"refresh_token": "GEbRxBN...edjnXbL",
"id_token": "eyJ0XAi...4faeEoQ",
"token_type": "Bearer"
}
Security Warning
11 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
Once the Access Token has been obtained it can be used to make
calls to the API by passing it as a Bearer Token in the
Authorization header of the HTTP request:
package main
import (
"fmt"
"net/http"
"io/ioutil"
)
func main() {
url := "https://someapi.com/api"
12 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
req.Header.Add("content-type",
"application/json")
req.Header.Add("authorization", "Bearer
ACCESS_TOKEN")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
fmt.Println(res)
fmt.Println(string(body))
HttpResponse<String> response =
Unirest.get("https://someapi.com/api")
.header("content-type", "application/json")
.header("authorization", "Bearer ACCESS_TOKEN")
.asString();
var options = {
method: 'GET',
url: 'https://someapi.com/api',
headers: {'content-type': 'application/json',
authorization: 'Bearer ACCESS_TOKEN'}
};
console.log(body);
13 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
});
#import <Foundation/Foundation.h>
NSMutableURLRequest *request =
[NSMutableURLRequest requestWithURL:[NSURL
URLWithString:@"https://someapi.com/api"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:10.0];
[request setHTTPMethod:@"GET"];
[request setAllHTTPHeaderFields:headers];
if (error) {
NSLog(@"%@", error);
}
else {
NSHTTPURLResponse *httpResponse =
14 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
(NSHTTPURLResponse *) response;
NSLog(@"%@", httpResponse);
}
}];
[dataTask resume];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://someapi.com/api",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer ACCESS_TOKEN",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
import http.client
15 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
conn = http.client.HTTPSConnection("someapi.com")
headers = {
'content-type': "application/json",
'authorization': "Bearer ACCESS_TOKEN"
}
res = conn.getresponse()
data = res.read()
print(data.decode("utf-8"))
require 'uri'
require 'net/http'
require 'openssl'
url = URI("https://someapi.com/api")
request = Net::HTTP::Get.new(url)
request["content-type"] = 'application/json'
request["authorization"] = 'Bearer ACCESS_TOKEN'
response = http.request(request)
puts response.read_body
import Foundation
let headers = [
16 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
"content-type": "application/json",
"authorization": "Bearer ACCESS_TOKEN"
]
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers
dataTask.resume()
Once your API receives a request with a Bearer Access Token, the
first thing to do is to validate the token. This consists of a series of
steps, and if any of these fails then the request must be rejected.
17 of 18 7/23/20, 10:54 AM
Execute an Authorization Code Grant Flow about:reader?url=https://auth0.com/docs/api-auth...
You can use Rules to change the returned scopes of the Access
Token and/or add claims to it (and the ID Token) with a script like
this:
context.accessToken['http://foo/bar'] =
'value';
context.idToken['http://fiz/baz'] = 'some other
value';
Refresh Tokens
Tokens
18 of 18 7/23/20, 10:54 AM