2012-02-13 23 views
16

私はsalesforce開発者サイトにあるサンプルを調べています。Salesforce REST APIログイン?

このサンプルでは、​​リンクをクリックすると、salesforceログインページにリダイレクトされます。ログインが成功すると、アクセストークンが発行されます。

私のアプリケーションでsalesforceログインページにリダイレクトしないでください。環境変数に設定されている既存のサンプルでは、​​

「https://login.salesforce.com」

私は、Salesforceのログインページにリダイレクトを避けるために何をすべき。

答えて

13

あなたが説明していることは、OAuthのように聞こえます(アクセストークンに言及したからです)。以下Salesforceで使用されているのOAuthの良い例があります

...

http://wiki.developerforce.com/page/Digging_Deeper_into_OAuth_2.0_at_Salesforce.com

+0

ご返信ありがとうございます。私は私の必要性のためにUsername-Passwordのoauthフローを使用しなければならないことがわかった。 https://login.salesforce.com/help/doc/en/remoteaccess_oauth_username_password_flow.htm =>これは参照する必要のあるリンクです。しかし、私はまだリダイレクトしている私のアプリの手順に従っている。 : –

+1

"oauth dance"の手順に正しく従っていますか?request-token-url、api-url、access-token-urlという3つのことが必要ですか? – david99world

+1

解決方法:こんにちは、私は問題に解決策を見つけました実際には、http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_REST_APIのリンクに記載されているサンプルを検証していました。その後、https://login.salesforce.com/helpのOAuth 2.0ユーザー名 - パスワードフローを実装しました。 /doc/ja/remoteaccess_oauth_username_password_flow.htm#send_up_response。私の問題を解決します。あなたの応答に感謝します。 –

5

SOLUTION:

こんにちはすべて、私は私の問題への解決策を到着しました。実際、私はリンクhttp://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_REST_APIで与えられたサンプルを調べていました。次に、https://login.salesforce.com/help/doc/en/remoteaccess_oauth_username_password_flow.htm#send_up_responseからのOAuth 2.0ユーザ名 - パスワードフローを実装しました。それは私の問題を解決する。

5

これは、ユーザ名・パスワードのOAuthフローを使用するサンプルJavaコードです:ジャワの場合

public class AccountQuery 
{ 
    // The connection data 
    private static final String query = "SELECT Name, Idfrom Account"; 
    private static final String clientId = "theID"; 
    private static final String clientSecret = "theSecret"; 
    // THis is meaningless in our context 
    private static final String redirectUri = "https://localhost:8443/_callback"; 
    private static final String environment = "https://login.salesforce.com"; 
    private static String tokenUrl = null; 
    private static final String username = "username"; 
    private static final String password = "passwordPlusSecret"; 
    private static String accessToken = null; 
    private static String instanceUrl = null; 

    public static void main(String[] args) 
    {  
     // Step 0: Connect to SalesForce. 
     System.out.println("Getting a token"); 
     tokenUrl = environment + "/services/oauth2/token"; 
     HttpClient httpclient = new HttpClient(); 
     PostMethod post = new PostMethod(tokenUrl);  
     post.addParameter("grant_type", "password"); 
     post.addParameter("client_id", clientId); 
     post.addParameter("client_secret", clientSecret); 
     post.addParameter("redirect_uri", redirectUri); 
     post.addParameter("username", username); 
     post.addParameter("password", password); 

     try { 
      httpclient.executeMethod(post); 
      try { 
       JSONObject authResponse = new JSONObject(new JSONTokener(new InputStreamReader(post.getResponseBodyAsStream()))); 
       System.out.println("Auth response: " + authResponse.toString(2)); 

       accessToken = authResponse.getString("access_token"); 
       instanceUrl = authResponse.getString("instance_url"); 

       System.out.println("Got access token: " + accessToken); 
      } catch (JSONException e) { 
       e.printStackTrace();     
      } 
     } catch (HttpException e1) { 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } finally { 
      post.releaseConnection(); 
     }  
     System.out.println("We have an access token: " + accessToken + "\n" + "Using instance " + instanceUrl + "\n\n"); 

     HttpClient httpclient = new HttpClient(); 
     GetMethod get = new GetMethod(instanceUrl + "/services/data/v28.0/query"); 

     // set the token in the header 
     get.setRequestHeader("Authorization", "OAuth " + accessToken); 

     // set the SOQL as a query param 
     NameValuePair[] params = new NameValuePair[1]; 

     params[0] = new NameValuePair("q",query); 
     get.setQueryString(params);  

     try { 
      httpclient.executeMethod(get); 
      if (get.getStatusCode() == HttpStatus.SC_OK) { 
       // Now lets use the standard java json classes to work with the results 
       JSONObject response = new JSONObject(new JSONTokener(new InputStreamReader(get.getResponseBodyAsStream()))); 
       System.out.println("Query response: "+ response.toString(2));//.substring(0, 500));     
       System.out.println(response.getString("totalSize") + " record(s) returned\n\n"); 
       JSONArray results = response.getJSONArray("records");    
       Account[] accounts = new Gson().fromJson(results.toString(), Account[].class); 
       return accounts; 
      } 
     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     }finally { 
      get.releaseConnection(); 
     } 
    } 
} 
関連する問題