Hi, I am having trouble using the Authorization code grant with node.js in APAC (Sydney). I have used the example provided in the tutorial which includes a ClientID and Secret. That works fine but to the US.
I assume I have to change to https://login.mypurecloud.com.au. Is that correct or does all login redirects go to the .com site only.
I have then created new ClientID and Secret as Authorize Code. Is that correct?
What URI's do I enter in the allowable redirects? I want to redirect to my localhost:8085.
I have tried several variations and I just can't get the the authorization to work in AU. I keep getting invalid or expired credentials. I have recreated these a few times. When I change back to example credentials and go back to .com it all works fine again.
Thanks
I assume I have to change to https://login.mypurecloud.com.au. Is that correct or does all login redirects go to the .com site only.
If you're using the AU environment, all requests must be made to *.com.au hosts. PureCloud data does not span regions, so orgs, users, and OAuth clients from one region will be completely unknown to another region. OAuth client IDs and secrets can only be used in the region in which they were created.
I have then created new ClientID and Secret as Authorize Code. Is that correct?
What URI's do I enter in the allowable redirects? I want to redirect to my localhost:8085
The redirect URI must be the exact URI that will be sent with authorization requests and that the user will be redirected back to. Even one character difference, like a trailing slash, will cause the redirect URI to be invalid.
Thanks Tim. Just so I can be clear. In the following code (which is as per example with .au added to each request.
What should the redirect URI be? http://localhost:8085/oauth2/callback
var http = require("http");
var express = require('express');
var app = express();
var uuid = require('node-uuid');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var request = require('request');
var client_id = 'my-client-id';
var client_secret = 'my-secret';
var authvalidation = function(req, res, next) {
console.log('\n['+req.method+' '+req.url+']');
//if we don't have a session then redirect them to the login page
if((req.cookies && !(req.cookies.session && sessionMap[req.cookies.session])) &&
req.url.indexOf("oauth") == -1){
//redirect the user to authorize with purecloud
var redirectUri = "https://login.mypurecloud.com.au/oauth/authorize?" +
"response_type=code" +
"&client_id=" + client_id +
"&redirect_uri=http://localhost:8085/oauth2/callback";
console.log("redirecting to " + redirectUri);
res.redirect(redirectUri);
return;
}
//if we do have a session, just pass along to the next http handler
console.log("have session")
next();
}
app.use(bodyParser.json());
app.use(cookieParser());
app.use(authvalidation);
app.use(express.static(__dirname));
var sessionMap ={};
app.get("/", function(req, res){
res.redirect("/my_info.html");
})
//this route handles the oauth callback
app.get("/oauth2/callback", function(req,res){
//the authorization page has called this callback and now we need to get the bearer token
console.log("oauth callback")
console.log(req.query.code)
var authCode = req.query.code;
var tokenFormData = {
grant_type: "authorization_code",
code: authCode, //from the query string parameters sent to this url
redirect_uri : "http://localhost:8085/oauth2/callback"
}
var postData = {
url:'https://login.mypurecloud.com.au/oauth/token',
form: tokenFormData,
auth: { //basic auth here
user: client_id,
pass: client_secret
}
}
//post back to /oauth/token with the client id and secret as well as the auth code that was sent to us.
request.post(postData, function(err,httpResponse,body){
console.log("got token data back: ")
console.log(body);
var tokenResponse = JSON.parse(body);
var sessionId = uuid.v4();
//store the session id as a key in the session map, the value is the bearer token for purecloud.
//we want to keep that secure so won't send that back to the client
sessionMap[sessionId] = tokenResponse.access_token;
//send the session id back as a cookie
res.cookie('session', sessionId);
res.redirect("/my_info.html");
});
});
//wrap up the api/v2/users/me call inside a /me route
app.get("/me", function(req, res){
//get the session from map using the cookie
var oauthId = sessionMap[req.cookies.session];
var getData = {
url:'https://api.mypurecloud.com.au/api/v2/users/me',
auth: {
bearer: oauthId
}
};
request.get(getData, function (e, r, user) {
console.log("Got response for /users/me");
console.log(user);
console.log(e);
res.send(user);
})
});
var httpServer = http.createServer(app);
httpServer.listen('8085');