[SalesForce] How to retrieve connected app using metadata API

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 .

Best Answer

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@email.com';

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 });
Related Topic