Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.4 kB
3
Indexable
Never
public static MA_MechanicsServiceREST.ResponseREST login() {
        String accessToken;
        if (Limits.getCallouts() >= Limits.getLimitCallouts()) {
            return new MA_MechanicsServiceREST.ResponseREST('callout limit exceeded');
        } else {
            czarek_org_pass__c cs = czarek_org_pass__c.getOrgDefaults();
            String requestBody =
                    'grant_type=password&client_id=' + cs.client_id__c +
                            '&client_secret=' + cs.client_secret__C +
                            '&username=' + cs.login__c +
                            '&password=' + cs.password__c + cs.token__c;
            Http http = new Http();
            HttpRequest request = new HttpRequest();
            request.setBody(requestBody);
            request.setMethod('POST');
            request.setEndpoint(cs.endpoint__c + '/services/oauth2/token');

            try {
                HttpResponse res = http.send(request);
                OAuth2 objAuthenticationInfo = (OAuth2) JSON.deserialize(res.getBody(), OAuth2.class);
                accessToken = objAuthenticationInfo.access_token;
                Cache.Org.put('local.TokenCache.token', accessToken);
            } catch (exception e) {
                return new MA_MechanicsServiceREST.ResponseREST(e.getMessage());
            }
        }
        return null;
    }