shuklavatsal1992

FirstTimeAuthXero

Dec 22nd, 2019
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. //(Only First time)THIS IS FIRST LEVEL AUTHENTICATION WHERE YOU WILL RECEIVE 'access_token' & 'refresh_token'..
  2. @future(callout = true)
  3. public static void authenticateUsingAuthCode() {
  4. XeroSettings xss = new XeroSettings();
  5. String authCode = xss.authCode;
  6. System.debug('MARK:==> authenticateUsingAuthCode() -- authenticating... using =' + authCode);
  7. //String encodeString = XeroConfig.Client_Id + ':'+ XeroConfig.Client_Secret;
  8. String encodeString = xss.clientId + ':' + xss.clientSecret;
  9. System.debug('MARK:==> authenticateUsingAuthCode() beforeEncoding =' + encodeString);
  10. String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf(encodeString));
  11. System.debug('MARK:==> authenticateUsingAuthCode() afterEncoding =' + authorizationHeader);
  12. String redirectUrl = 'https://www.zulu8.com';
  13. String body = 'grant_type=authorization_code&code=' + authCode + '&redirect_uri=' + redirectUrl;
  14.  
  15. //--------------------
  16. HttpRequest request1 = new HttpRequest();
  17. request1.setEndpoint('https://identity.xero.com/connect/token');
  18. request1.setMethod('POST');
  19. request1.setHeader('Authorization', authorizationHeader);
  20. request1.setHeader('Content-Type', 'application/x-www-form-urlencoded');
  21. request1.setBody(body);
  22. request1.setHeader('Content-Length', String.valueOf(body.length()));
  23. //---------------------------
  24. Http h1 = new Http();
  25. HttpResponse res1 = h1.send(request1);
  26. System.debug('MARK:--> authenticateUsingAuthCode() =' + res1.getBody());
  27. AccessToken accessTokenRes = AccessToken.parse(res1.getBody());
  28. if (accessTokenRes != null && accessTokenRes.access_token.length() > 0) {
  29. System.debug('MARK:==> authenticateUsingAuthCode() -- finished....Access_Token =' + accessTokenRes.access_token + ' Refresh_Token =' + accessTokenRes.refresh_token);
  30. String token = accessTokenRes.access_token;
  31. String method = 'GET';
  32. String endpoint = 'https://api.xero.com/connections';
  33. HTTPRequest req = new HTTPRequest();
  34. req.setHeader('Content-Type', 'application/json'); //setHeader('Content-Type','application/x-www-form-urlencoded');
  35. req.setHeader('Authorization', 'Bearer ' + token);
  36. req.setMethod(method);
  37. req.setEndpoint(endpoint);
  38. HTTP h = new HTTP();
  39. HTTPResponse res = h.send(req);
  40. System.debug('MARK:==> getTenant() response =' + res.getBody());
  41. Tenant tnnt = Tenant.parse(res.getBody());
  42. if (tnnt != null) {
  43. xss.addTenant(tnnt);
  44. xss.accessToken = accessTokenRes.access_token;
  45. xss.refreshToken = accessTokenRes.refresh_token;
  46. xss.expirySeconds = accessTokenRes.expires_in;
  47. xss.authCode = authCode;
  48. xss.save();
  49. }
  50. return;
  51. }
  52. System.debug('MARK:==> authenticateUsingAuthCode() -- not finished.... Response =' + res1.getBody());
  53. return;
  54. }
Add Comment
Please, Sign In to add comment