簡體   English   中英

使用帶有Java的Rest API進行Salesforce身份驗證

[英]Salesforce authentication using Rest API with JAVA

我想使用Salesforce Rest API和JAVA對Salesforce進行身份驗證。

為此,我遵循以下URL

http://www.asagarwal.com/2401/step-by-step-guide-to-get-started-with-salesforce-rest-api-using-java

我的Java代碼如下

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONObject;
import org.json.JSONTokener;
import org.json.JSONException;

public class OrderProcessing extends HttpServlet {
    private static final long serialVersionUID = 1L;
    static final String PASS = "XXXXXXX";
    static final String SecurityToken = "XXXXXXXXXXXX";
    static final String USERNAME = "adminuser@.salesforce.com";
    static final String PASSWORD = PASS + SecurityToken;
    static final String LOGINURL = "https://login.salesforce.com";
    static final String GRANTSERVICE = "/services/oauth2/token?grant_type=password";
    static final String CLIENTID = "ConsumerKeyFromSalesfoceConnectedApps";
    static final String CLIENTSECRET = "ConsumerSecretFromSalesforceConnectedApps";

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpClient httpclient = HttpClientBuilder.create().build();
        String loginURL = LOGINURL + GRANTSERVICE + "&client_id=" + CLIENTID + "&client_secret=" + CLIENTSECRET
                + "&username=" + USERNAME + "&password=" + PASSWORD;

        HttpPost httpPost = new HttpPost(loginURL);
        HttpResponse resp = null;

        try {
            resp = httpclient.execute(httpPost);
        } catch (ClientProtocolException cpException) {
            cpException.printStackTrace();
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        final int statusCode = resp.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) {
            System.out.println("Error authenticating to Force.com: " + statusCode);
            return;
        }

        String getResult = null;
        try {
            getResult = EntityUtils.toString(resp.getEntity());
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

        JSONObject jsonObject = null;
        String loginAccessToken = null;
        String loginInstanceUrl = null;
        try {
            jsonObject = (JSONObject) new JSONTokener(getResult).nextValue();
            loginAccessToken = jsonObject.getString("access_token");
            loginInstanceUrl = jsonObject.getString("instance_url");
        } catch (JSONException jsonException) {
            jsonException.printStackTrace();
        }

        System.out.println(resp.getStatusLine());
        System.out.println("Successful login");
        System.out.println("  instance URL: " + loginInstanceUrl);
        System.out.println("  access token/session ID: " + loginAccessToken);

        httpPost.releaseConnection();
    }
}

響應: HttpResponseProxy{HTTP/1.1 400 Bad Request [Date: Thu, 06 Oct 2016 11:25:26 GMT, Strict-Transport-Security: max-age=10886400; includeSubDomains; preload, Content-Security-Policy-Report-Only: default-src https:; script-src https: 'unsafe-inline' 'unsafe-eval'; style-src https: 'unsafe-inline'; img-src https: data:; font-src https: data:; report-uri /_/ContentDomainCSPNoAuth?type=login, Set-Cookie: BrowserId=bIo6TZOBQRSS2KFKtUR5ZA;Path=/;Domain=.salesforce.com;Expires=Mon, 05-Dec-2016 11:25:26 GMT, Expires: Thu, 01 Jan 1970 00:00:00 GMT, Pragma: no-cache, Cache-Control: no-cache, no-store, X-ReadOnlyMode: false, Content-Type: application/json;charset=UTF-8, Transfer-Encoding: chunked] ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}} HttpResponseProxy{HTTP/1.1 400 Bad Request [Date: Thu, 06 Oct 2016 11:25:26 GMT, Strict-Transport-Security: max-age=10886400; includeSubDomains; preload, Content-Security-Policy-Report-Only: default-src https:; script-src https: 'unsafe-inline' 'unsafe-eval'; style-src https: 'unsafe-inline'; img-src https: data:; font-src https: data:; report-uri /_/ContentDomainCSPNoAuth?type=login, Set-Cookie: BrowserId=bIo6TZOBQRSS2KFKtUR5ZA;Path=/;Domain=.salesforce.com;Expires=Mon, 05-Dec-2016 11:25:26 GMT, Expires: Thu, 01 Jan 1970 00:00:00 GMT, Pragma: no-cache, Cache-Control: no-cache, no-store, X-ReadOnlyMode: false, Content-Type: application/json;charset=UTF-8, Transfer-Encoding: chunked] ResponseEntityProxy{[Content-Type: application/json;charset=UTF-8,Chunked: true]}}

使用Chrome的Rest Web Service客戶端擴展的響應: {"error":"invalid_client_id","error_description":"client identifier invalid"}

請幫助我解決此問題。

我使用以下代碼解決了我的問題:

String loginURL = LOGINURL + GRANTSERVICE + "&client_id=" + CLIENTID + "&client_secret=" + CLIENTSECRET + "&username=" + USERNAME + "&password=" + PASSWORD;

代替

String loginURL = LOGINURL + GRANTSERVICE + "&client_id=" + CLIENTID + "&client_secret=" + CLIENTSECRET
                + "&username=" + USERNAME + "&password=" + PASSWORD;

以前有用嗎? 如果是,則需要再次進行身份驗證。

新的聯網應用程序安裝大約需要15分鍾。 由於該錯誤表明客戶端ID不好,所以我想這就是問題所在。 你可以再試一次嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM