AnsweredAssumed Answered

POST /login/oauth2/token Error response - java API

Question asked by prasanna k on Mar 3, 2016
Latest reply on Feb 27, 2017 by Monish Verma

Hi,

As Per the OAuth2 Endpoints - Canvas LMS REST API Documentation  I just follow the 3rd step in the OAuth 2 Access token getting from canvas LMS.

See Section 4.1.3 of the OAuth2 RFC for more information about this process.

POST /login/oauth2/token

Parameters

ParameterDescription
grant_type Values currently supported "authorization_code"
client_id The client id for your registered application.
client_secret The client secret for your registered application.
redirect_uri If a redirect_uri was passed to the initial request in step 1, the same redirect_uri must be given here.
code
Required if grant_type is authorization_code. The code you received in a redirect response.

 

"HTTP/1.1 400 Bad Request[\r][\n]"

"{"error":"invalid_request","error_description":"client does not have access to specified account","message":"invalid code"}[\r][\n]"

 

HttpClient client = HttpClientBuilder.create().build();

  HttpPost post = new HttpPost(ConsumerUrl);

  List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();

  //hard coded for testing

  urlParameters.add(new BasicNameValuePair("grant_type", "authorization_code"));

  urlParameters.add(new BasicNameValuePair("client_id", clientId));

  urlParameters.add(new BasicNameValuePair("client_secret", clientSecret));

  urlParameters.add(new BasicNameValuePair("redirect_uri", contextPath));

  urlParameters.add(new BasicNameValuePair("code", code));

  post.setEntity(new UrlEncodedFormEntity(urlParameters));

  HttpResponse httpResponse  client.execute(post);

String textView="";

  BufferedReader rd = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));

  if(httpResponse.getStatusLine().getStatusCode()==200){

  String line = "";

  while ((line = rd.readLine()) != null) {

  textView+=line;

  }

  JSONObject resjson=new JSONObject(textView);

  partnerConfig.setAccessToken(resjson.getString("access_token"));

  partnerConfig.setRefreshToken(resjson.getString("refresh_token"));

  partnerConfig.setHostStatus("true");

  }else{

  out.println("Error response from canvas response :"+httpResponse.getStatusLine().getStatusCode());

  }

 

And for Https protocol facing time out error.

Outcomes