• mannsandeep
  • NEWBIE
  • 0 Points
  • Member since 2009

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 13
    Replies
trying to get access authorization token thru java code but salesforce either return "internal server error - 500". 
Same URL and parameters work fine from postman 

public class RestWork
{
    private static final String CONSUMER_KEY = "3MVG9PbXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXzCH";
    private static final String CONSUMER_SECRET = "11111111111111111111";
       
    public static void main(String[] args) throws IOException
    {
              HttpClient client =  new DefaultHttpClient();;
              String loginURL = "https://cs51.salesforce.com/services/oauth2/token";    
        
        HttpPost httpPost = new HttpPost(loginURL);
       
     params.add(new BasicNameValuePair("client_id",  URLEncoder.encode(CONSUMER_KEY,"UTF-8")));
     params.add(new BasicNameValuePair("client_secret",  URLEncoder.encode(CONSUMER_SECRET,"UTF-8")));
     params.add(new BasicNameValuePair("grant_type", URLEncoder.encode("password","UTF-8")));
     params.add(new BasicNameValuePair("username", "nCCCCC@XXXXXX.com"));
     params.add(new BasicNameValuePair("password",  "PPPPPPPPPPP!23dddddddddddddddddddddddddd"));
  
    httpPost.setEntity(new UrlEncodedFormEntity(params));
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        HttpResponse response = client.execute(httpPost);
       String body = EntityUtils.toString(response.getEntity());
         System.out.println(body);
      }
}

Hi, I am trying to get the access token in Java and following the instructions on http://www.salesforce.com/us/developer/docs/chatterapi/index.htm
But I continuously get this error:

{"error":"unsupported_grant_type","error_description":"grant type not supported"}

 

My codes are here:

 

 

package admin;

 

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

 

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

 

import com.google.gson.Gson;

 

public class REST
{
String authURL = "https://login.salesforce.com/services/oauth2/authorize";
String tokenURL = "https://na14.salesforce.com/services/oauth2/token";
public static void main(String[] args) throws ClientProtocolException, IOException
{
REST rest = new REST();
}

public REST() throws ClientProtocolException, IOException
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(tokenURL);
post.getParams().setParameter("client_id", "3MVG9rFJvQRVOvk6sl7xMXtrbyQb2XUv2vprLKWv0uxBLKFzNqtHJG6cmzuemPYDB3hPwqXsBlw0BGQPkb81P");
post.getParams().setParameter("client_secret", "4864720658246552438");
post.getParams().setParameter("grant_type", "password");
post.getParams().setParameter("username", "XXXX");
post.getParams().setParameter("password", "XXXX");
post.getParams().setParameter("redirect_uri", "https://login.salesforce.com/services/oauth2/success");
HttpResponse response = httpclient.execute(post);
JSONObject json = (JSONObject) JSONValue.parse(new InputStreamReader (response.getEntity().getContent()));
System.out.println(json);
}
}

 

I haven't had much luck getting SSO to work with my SAML assertion. Has anyone got this to work? If so, what does your saml response look like?  I signed my assertion and I believe everything is correct, yet the login history gives me "Failed: Assertion Invalid"

Any ideas?

Thank you.