如何在不登录salesforce的情况下访问Salesforce
原生应用中的android
条记录。我使用了已连接的app(Salesforce)
方法,但在Android应用中首先要求我在salesforce
之后登录,而不是访问Salesforce
条记录。
有没有办法在Android应用程序中无需登录才能访问Salesforce记录?
答案 0 :(得分:0)
对于没有登录凭据的Android Native App中的Access Salesforce记录,您首先需要从salesforce获取访问令牌。
要获取访问令牌,您可以点击此链接。 https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_oauth.htm
获取令牌后,您需要进行REST调用才能获得Salesforce Records。
package com.example.admin.myapplication;
公共类MainActivity扩展了Activity {
// The connection data
private static final String query = "SELECT Name, Id from Account";
private static final String clientId = "Your salesforce consumer Key";
private static final String clientSecret = "Secret Key";
// THis is meaningless in our context
private static final String redirectUri = "Redirect URI";
//private static final String environment = "https://harishgakhar40-dev-ed.my.salesforce.com";
private static final String environment = "https://login.salesforce.com";
private static String tokenUrl = null;
private static final String username = "salesforce Username";
private static final String password = "salesforce password + security token";
private static String accessToken = null;
private static String instanceUrl = null;
private ArrayAdapter<String> listAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create list adapter
listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
((ListView) findViewById(R.id.contacts_list)).setAdapter(listAdapter);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
// Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
tokenUrl = environment + "/services/oauth2/token?grant_type=password&client_id=" + clientId + "&client_secret=" + clientSecret + "&username=" + username + "&password=" + password;
HttpPost httpPost = new HttpPost(tokenUrl);
// Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
nameValuePair.add(new BasicNameValuePair("Content-Type", "application/json"));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
} catch (UnsupportedEncodingException e){
// writing error to Log
e.printStackTrace();
}
JSONTokener tokener;
String accessToken,instanceURL;
JSONArray finalResult;
// Making HTTP Request
try {
HttpResponse response = httpClient.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONObject jObj = new JSONObject(json);
accessToken = jObj.getString("access_token");
instanceURL = jObj.getString("instance_url");
getAccountData(accessToken, instanceURL);
} catch (Exception e) {
e.getMessage();
}
}
public void getAccountData(String access_token, String instance_url) {
DefaultHttpClient client = new DefaultHttpClient();
String url = instance_url + "/services/data/v20.0/query/?q=";
String soqlQuery = "Select Id, Name, BillingStreet, BillingCity, BillingState From Account ";
try
{
url += URLEncoder.encode(soqlQuery, "UTF-8");
}
catch(UnsupportedEncodingException e){}
HttpGet getRequest = new HttpGet(url);
getRequest.addHeader("Authorization", "OAuth " + access_token);
try {
HttpResponse response = client.execute(getRequest);
String result = EntityUtils.toString(response.getEntity());
JSONObject object = (JSONObject) new JSONTokener(result).nextValue();
JSONArray records = object.getJSONArray("records");
listAdapter.clear();
for (int i=0;i<records.length();i++) {
JSONObject record = (JSONObject) records.get(i);
String accountName = record.getString("Name");
Log.v("accountName---- ","accountName ---- "+accountName);
listAdapter.add(accountName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}