Asked by:
Office 365 API get access token for send mail through API but throwing 'invalid request' 'The request body must contain parameter:'grant_type'

Question
-
Hi Team,
I'm trying to integrate office 365 API by using 'v2 app model preview' for a sample app in android. For every API call access_token is mandatory. For that retrieved auth code successfully. But trying to acquire access_token in JSON post method throwing "invalid request. The request body must contain parameter 'grant_type' ".
Register an app in portal and retrieved client_id and redirect_uri(hardcoded one) for mobile platform.
Even i'm passing grant_type,authcode,client_id,scope parameter in jsonobject. But still showing above error. Please let me know where what i'm doing wrong. Any kind of help will be appreciable. Thanks a lot in advance.
Below is my format JSON format to post in method body:
"{\"grant_type\":\"authorization_code\",\"client_id\":\"afcee442-ab52-4794-a718-390ca6377ad0\",\"redirect_uri\":\"urn:ietf:wg:oauth:2.0:oob\",\"scope\":\"https://outlook.office.com/mail.send\",\"code\":"+auth+"}"
Note:Value "auth"(Authorization code) will generated dynamically while login
Thanks,
Sriram.M
Wednesday, October 21, 2015 10:57 AM
All replies
-
Hi Sriram,
Could you share us how you post your request when you want to access the access_token?
I found a helpful link as below.
Reference:https://msdn.microsoft.com/en-us/office/office365/howto/authentication-v2-protocols
I will try to make some test with this, it would be some delay.
Thanks for your understanding.
Best Regards,
Edward
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- Edited by Edward8520Microsoft contingent staff Friday, October 23, 2015 9:14 AM
Friday, October 23, 2015 3:33 AM -
Hi Edward,
Below is my post request to get access_token. "auth"(Authorization code)value will generate dynamically every time we login.
In pre execute method in AsyncTask will call below method to generate access_token through JSON format but fails to return.
Returning error code "400". Bad Request. The request body must contain parameter 'grant_type' ".
public String getjsonfromurl(String url) {
InputStream is = null;
String json = "";
JSONObject jObj = null;
String result = "";
Log.e("URL", url);
String response = "";
JSONObject jsonobj = new JSONObject();
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://login.microsoftonline.com/common/oauth2/v2.0/token");
String auth = "\"" + authCode + "\"";
String format = "{\"grant_type\":\"authorization_code\",\"client_id\":\"afcee442-ab52-4794-a718-390ca6377ad0\",\"redirect_uri\":\"urn:ietf:wg:oauth:2.0:oob\",\"scope\":\"https://outlook.office.com/mail.send\",\"code\":" + auth + "}";
StringEntity input = new StringEntity(format);
httpPost.setHeader("Content-Type", "application/json");
httpPost.setEntity(input);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpResponse.getStatusLine().getStatusCode() == 202) {
result = EntityUtils.toString(httpEntity);
Log.e("RESULT", result);
response = "SUCCESS";
} else if (httpResponse.getStatusLine().getStatusCode() == 401) {
response = "UNAUTHORIZED";
} else if (httpResponse.getStatusLine().getStatusCode() == 400) {
response = "BADREQUEST";
} else {
response = "NONE";
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}Monday, October 26, 2015 6:59 AM -
Hi Sriram,
I found you posted a new thread below which is about returning refresh_token.
# Office 365 API not returning refresh_token when tried to get access_token using v2 approach
https://social.msdn.microsoft.com/Forums/en-US/102c7261-f3c1-4213-a64b-fbb9e1325c40/office-365-api-not-returning-refreshtoken-when-tried-to-get-accesstoken-using-v2-approach?forum=accessdev
Did you mean your original issue about access_token has been resolved? If you have, it would be appreciated if you could share us your solution, and it would be helpful if others run into the same issue.
Best Regards,
Edward
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.Thursday, October 29, 2015 3:04 AM -
Hi Edward,
Sorry for the confusion will explain in a precise manner.
First tried in a above manner by using json approach in post request. But it fails to return response throwing an above error "Invalid Request. The request body must contain the following parameter "grant_type"". As per documentation used json format but no response so raised a question in developer forum.
Then proceed further with url-encoded approach by passing parameters in List<NameValuePairs> its successfully returned response but problem is not returning 'refresh_token'. But as per documentation will return refresh_token parameter but didn't get in response. So raised another question in new thread as you mentioned in above reply.
As per documentation lifetime for access_token is 1 hour so once its expires we will call refresh token API by using 'refresh_token' parameter it will return newly issued refresh token which is long lived. But due to unavailability of 'refresh_token' parameter needs to login with credentials once again to get 'access_token' to send mail which we need to send for authentication.Below is my sample post request in url-encoded form:
protected JSONObject doInBackground(String... params) {
List<NameValuePair> parmas = new ArrayList<NameValuePair>();
parmas.add(new BasicNameValuePair("grant_type", "authorization_code"));
parmas.add(new BasicNameValuePair("redirect_uri", "urn:ietf:wg:oauth:2.0:oob"));
parmas.add(new BasicNameValuePair("client_id", "afcee442-ab52-4794-a718-390ca6377ad0"));
parmas.add(new BasicNameValuePair("scope", "https://outlook.office.com/mail.read https://outlook.office.com/mail.send"));
//parmas.add(new BasicNameValuePair("scope", "https://graph.windows.net/directory.read https://graph.windows.net/directory.write"));
parmas.add(new BasicNameValuePair("code",
authCode));
// parmas.add(new BasicNameValuePair("client_secret","QWL9gdo55j5bDWqrZ87aWfo"));
JSONObject json = getjsonfromurl("https://login.microsoftonline.com/common/oauth2/v2.0/token", parmas);
return json;
}POST REQUEST:
public JSONObject getjsonfromurl(String url, List<NameValuePair> params) {
InputStream is = null;
String json = "";
JSONObject jObj = null;
String result = "";
Log.e("URL", url);
JSONObject jsonobj = new JSONObject();
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
Log.e("AUTHCODE", authCode);
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
Log.e("JSONOBJECT", jObj + "");
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}- Edited by Sriram.M Thursday, October 29, 2015 11:53 AM
Thursday, October 29, 2015 11:51 AM