6

I have created connected App using metadata API,Now I have to retrieve connected App's Information using metadata API,The code is working fine for the Apps which is created manually but not for the Apps which is created through metadata .I have provided full access for both connected Apps.

public static void retrieveConnectedApp() {
    MetadataService.MetadataPort service = new MetadataService.MetadataPort();
    service.SessionHeader = new MetadataService.SessionHeader_element();
    service.SessionHeader.sessionId = UserInfo.getSessionId();
    MetadataService.ConnectedApp connectedApp =
            (MetadataService.ConnectedApp)service.readMetadata('ConnectedApp', new String[] {'ActivePrime' }).getRecords()[0];
}

Is there any security issue ? For Oauth authentication, connected app (manually) is working fine but connected app (Metadata API) failed .

7
  • Have you define profile settings (objects accessible through connected App) for users which will be accessing data? Feb 16, 2017 at 8:55
  • Connected App is not an object itself . what profile setting I am supposed to do? Feb 16, 2017 at 10:30
  • I've already answered this question. Please visit this link Feb 16, 2017 at 10:31
  • I just went through given link . So where I can provide those permissions ? Feb 16, 2017 at 11:26
  • @AjayPrakashDubey, so a Connected App that you created in the UI is coming back with that Apex code, but not one that you created via the Metadata API? Are there any errors, or is the latter just not returning a MetadataService.ConnectedApp? Feb 21, 2017 at 19:26

2 Answers 2

1

Maybe somebody needs the code which creates connected app.

MetadataService.MetadataPort service = createService();
MetadataService.ConnectedApp connectedApp = new 
MetadataService.ConnectedApp();

connectedApp.label = 'Test 005';
connectedApp.fullName = 'Test_005';
connectedApp.contactEmail = '[email protected]';

MetadataService.ConnectedAppOauthConfig oauthConfig = new 
MetadataService.ConnectedAppOauthConfig();

oauthConfig.consumerKey = 'yourConsumerKey';
oauthConfig.consumerSecret = 'yourConsumerSecret';
oauthConfig.scopes = new List<String>{'Basic', 'Api', 'Web', 'Full'};
oauthConfig.callbackUrl = 'https://www.google.com/';

connectedApp.oauthConfig = oauthConfig;

List<MetadataService.SaveResult> results = service.createMetadata(new 
MetadataService.Metadata[] { connectedApp });
0

If you don't want to use Metadata API library, you could use the following snippet of code

private static String getSoapBodyXml(String endpoint, String name) {
    return ''
        + '<?xml version="1.0" encoding="utf-8"?>'
        + '<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
        + '<env:Header>'
        + '<urn:SessionHeader xmlns:urn="http://soap.sforce.com/2006/04/metadata">'
        + '<urn:sessionId>' + userInfo.getSessionId() + '</urn:sessionId>'
        + '</urn:SessionHeader>'
        + '</env:Header>'
        + '<env:Body>'
        + '<createMetadata xmlns="http://soap.sforce.com/2006/04/metadata">'
        + '<metadata xsi:type="ConnectedApp">'
        + '<fullName>' + name + String.valueOf(DateTime.now().getTime()).right(4) + '</fullName>'
        + '<label>' + name + String.valueOf(DateTime.now().getTime()).right(4) + '</label>'
        + '<contactEmail>[email protected]</contactEmail>'
        + '<oauthConfig>'+
        + '<callbackUrl>' + endpoint + '</callbackUrl>'
        + '<scopes>Full</scopes>'
        + '<scopes>RefreshToken</scopes>'
        + '</oauthConfig>'
        + '</metadata>'
        + '</createMetadata>'
        + '</env:Body>'
        + '</env:Envelope>'
    ;
}

public static HttpResponse add(String endpoint, String name) {
    HttpRequest req = new HttpRequest();
    req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/Soap/m/50.0');
    req.setMethod('POST');
    req.setHeader('Content-Type', 'text/xml');
    req.setHeader('SOAPAction', '""');
    req.setBody(getSoapBodyXml(endpoint, name));
    HttpResponse r = new Http().send(req);
    System.debug('add getBody: ' + r.getBody());
    System.debug('add getStatus: ' + r.getStatus());
    System.debug('add getStatusCode: ' + r.getStatusCode());
    val(r);
    return r; 
}
private static void val(HttpResponse r) {
    System.debug('success? ' + r.getBody().contains('<success>true</success>'));
    if (!r.getBody().contains('<success>true</success>')) {
        throw new UnexpectedException(r.getBody().substringBetween('<statusCode>', '</statusCode>') + ': ' + r.getBody().substringBetween('<message>', '</message>'));                  
    }
}
public static HttpResponse deploy(Integer i) {
    return add(
        'https://google.com',
        'DeployedApp' + i
    );
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .