Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //(Only First time)THIS IS FIRST LEVEL AUTHENTICATION WHERE YOU WILL RECEIVE 'access_token' & 'refresh_token'..
- @future(callout = true)
- public static void authenticateUsingAuthCode() {
- XeroSettings xss = new XeroSettings();
- String authCode = xss.authCode;
- System.debug('MARK:==> authenticateUsingAuthCode() -- authenticating... using =' + authCode);
- //String encodeString = XeroConfig.Client_Id + ':'+ XeroConfig.Client_Secret;
- String encodeString = xss.clientId + ':' + xss.clientSecret;
- System.debug('MARK:==> authenticateUsingAuthCode() beforeEncoding =' + encodeString);
- String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(encodeString));
- System.debug('MARK:==> authenticateUsingAuthCode() afterEncoding =' + authorizationHeader);
- String redirectUrl = 'https://www.zulu8.com';
- String body = 'grant_type=authorization_code&code=' + authCode + '&redirect_uri=' + redirectUrl;
- //--------------------
- HttpRequest request1 = new HttpRequest();
- request1.setEndpoint('https://identity.xero.com/connect/token');
- request1.setMethod('POST');
- request1.setHeader('Authorization', authorizationHeader);
- request1.setHeader('Content-Type', 'application/x-www-form-urlencoded');
- request1.setBody(body);
- request1.setHeader('Content-Length', String.valueOf(body.length()));
- //---------------------------
- Http h1 = new Http();
- HttpResponse res1 = h1.send(request1);
- System.debug('MARK:--> authenticateUsingAuthCode() =' + res1.getBody());
- AccessToken accessTokenRes = AccessToken.parse(res1.getBody());
- if (accessTokenRes != null && accessTokenRes.access_token.length() > 0) {
- System.debug('MARK:==> authenticateUsingAuthCode() -- finished....Access_Token =' + accessTokenRes.access_token + ' Refresh_Token =' + accessTokenRes.refresh_token);
- String token = accessTokenRes.access_token;
- String method = 'GET';
- String endpoint = 'https://api.xero.com/connections';
- HTTPRequest req = new HTTPRequest();
- req.setHeader('Content-Type', 'application/json'); //setHeader('Content-Type','application/x-www-form-urlencoded');
- req.setHeader('Authorization', 'Bearer ' + token);
- req.setMethod(method);
- req.setEndpoint(endpoint);
- HTTP h = new HTTP();
- HTTPResponse res = h.send(req);
- System.debug('MARK:==> getTenant() response =' + res.getBody());
- Tenant tnnt = Tenant.parse(res.getBody());
- if (tnnt != null) {
- xss.addTenant(tnnt);
- xss.accessToken = accessTokenRes.access_token;
- xss.refreshToken = accessTokenRes.refresh_token;
- xss.expirySeconds = accessTokenRes.expires_in;
- xss.authCode = authCode;
- xss.save();
- }
- return;
- }
- System.debug('MARK:==> authenticateUsingAuthCode() -- not finished.... Response =' + res1.getBody());
- return;
- }
Add Comment
Please, Sign In to add comment