Scenario: Connecting Your App to Twitter (Full Code Example)
Included here is the full code for the example scenario. You can use this as a reference when setting up your own API services.
framework.Api.Service service = new framework.Api.Service();
service.name = 'Twitter';
service.endpoint ='https://api.twitter.com';
service.isEnabled = true ;
service.debugMode = true ;
service.apexClass = 'framework.VendorAuthenticationService';
service.namespace = 'N/A';
framework.Config.push(service);
framework.Api.ServiceConnection serviceConnection = new
framework.Api.ServiceConnection();
serviceConnection.configId = 'User';
serviceConnection.clientId = 'QKXzYfvemU3AwfXNPjjv3SFWA';
serviceConnection.clientSecret =
'c5SJLeluRYMQlWEEO5wDflrP6IPLHNk0xUawuaBNE88pGXFXbX';
serviceConnection.isDefault = true;
serviceConnection.apiService = service.name;
framework.Config.push(serviceConnection);
framework.Api.ServiceConnectionConfig connectionConfig = new
framework.Api.ServiceConnectionConfig();
connectionConfig.apiServiceConnection = serviceConnection.configId;
connectionConfig.name = 'AUTHTYPECONFIG';
connectionConfig.value =
framework.VendorAuthenticationservice.AuthType.OAuth1A.name();
framework.Config.push(connectionConfig);
connectionConfig = new framework.Api.ServiceConnectionConfig();
connectionConfig.apiServiceConnection = serviceConnection.configId;
connectionConfig.name = 'HTTPMETHODGETTOKEN';
connectionConfig.value = 'POST';
framework.Config.push(connectionConfig);
connectionConfig = new framework.Api.ServiceConnectionConfig();
connectionConfig.apiServiceConnection = serviceConnection.configId;
connectionConfig.name =
framework.VendorAuthenticationservice.OAuth1AURLS.AuthCodeURL.name();
connectionConfig.value = 'https://api.twitter.com/oauth/access_token';
framework.Config.push(connectionConfig);
connectionConfig = new framework.Api.ServiceConnectionConfig();
connectionConfig.apiServiceConnection = serviceConnection.configId;
connectionConfig.name =
framework.VendorAuthenticationservice.OAuth1AURLS.TokenURL.name();
connectionConfig.value = 'https://api.twitter.com/oauth/authorize';
framework.Config.push(connectionConfig);
connectionConfig = new framework.Api.ServiceConnectionConfig();
connectionConfig.apiServiceConnection = serviceConnection.configId;
connectionConfig.name =
framework.VendorAuthenticationservice.OAuth1AURLS.RequestTokenURL.nam
e();
connectionConfig.value = 'https://api.twitter.com/oauth/request_token';
framework.Config.push(connectionConfig);
framework.Api.Resource resource = new framework.Api.Resource();
resource.apiService = service.name;
resource.configId = 'Hashtags';
resource.apiObject = 'statuses';
resource.targetSObject = 'g10framework__Twitter_Tweet__c';
resource.apiObjectKeyField = 'id_str';
resource.targetObjectKeyField = 'g10framework__Tweet_Id__c';
resource.readURI = '/1.1/search/tweets.json';
resource.readVerb = 'GET';
framework.Config.push(resource);
framework.Api.Mapping mapping = new framework.Api.Mapping();
mapping.apiResource = resource.configId;
mapping.apiObject = resource.apiObject;
mapping.apiField = 'id_str';
mapping.targetField = 'g10framework__Tweet_Id__c';
mapping.targetFieldType = DisplayType.String.name();
framework.Config.push(mapping);
mapping = new framework.Api.Mapping();
mapping.apiResource = resource.configId;
mapping.apiObject = resource.apiObject;
mapping.apiField = 'text';
mapping.targetField = 'g10framework__Tweet__c';
mapping.targetFieldType = DisplayType.String.name();
framework.Config.push(mapping);
mapping = new framework.Api.Mapping();
mapping.apiResource = resource.configId;
mapping.apiObject = resource.apiObject;
mapping.apiField = 'user.screen_name';
mapping.targetField = 'g10framework__Twitter_Handle__c';
mapping.targetFieldType = DisplayType.String.name();
framework.Config.push(mapping);
mapping = new framework.Api.Mapping();
mapping.apiResource = resource.configId;
mapping.apiObject = resource.apiObject;
mapping.apiField = 'created_at';
mapping.targetField = 'g10framework__Tweet_Date__c';
mapping.targetFieldType = DisplayType.String.name();
framework.Config.push(mapping);
framework.Api.Variable variable = new framework.Api.Variable();
variable.apiResource = resource.configId;
variable.fieldApiName = 'q';
variable.required = true ;
framework.Config.push(variable);
variable = new framework.Api.Variable();
variable.apiResource = resource.configId;
variable.fieldApiName = 'count';
variable.defaultValue = '100';
variable.required = false ;
framework.Config.push(variable);
framework.RegisteredObject adPubReq = new
framework.RegisteredObject();
adPubReq.sObjectName =
framework.SchemaService.getDescribe(Twitter_Tweet__c.SObjectType).get
Name();
adPubReq.namespace = 'g10framework';
adPubReq.isEnabled = true;
adPubReq.isStandard = false;
adPubReq.triggersEnabled = true;
adPubReq.soqlLimit = String.valueOf(2000);
adPubReq.soqlOrderBy =
SObjectType.Twitter_Tweet__c.fields.Name.getName() + ' ASC';
framework.Config.push(adPubReq);
CODE