Able to generate Access token but while testing it is returning 401- Unauthorized











up vote
-1
down vote

favorite
1












I used the below code to generate access token and generated access token but not refresh token i'm getting 400 bad request for refresh token.



static String tenantId = "xxxxxxxxxxxxxxxxxxxxxxx";
static String username = "xxxxxxxxxxxxxxxxxxxxxxx";
static String password = "xxxxxxxxx";
static String clientId = "xxxxxxxxxxxxxxxxxxxxxxx";
static String resource = "https://dev.sharepoint.com";
static String userEmail = "xxxxxxxxxxxxxxxxxxxxxx";
static String clientSecret = "xxxxxxxxxxxxxxxxxxx";

public static void main(String args) throws MalformedURLException, IOException {
AuthenticationContext authContext = null;
AuthenticationResult authResult = null;
ExecutorService service = null;

try {
service = Executors.newFixedThreadPool(1);
String url = "https://login.microsoftonline.com/" + tenantId + "/oauth2/authorize";
authContext = new AuthenticationContext(url, false, service);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = authContext.acquireToken(resource, credential, null);

authResult = future.get();
System.out.println("get access token: n" + authResult.getAccessToken());
System.out.println("get refresh token: n" + authResult.getRefreshToken());
} catch (Exception ex) {
ex.printStackTrace();
}
// get access token by refresh token
getToken(authResult.getRefreshToken());
}

public static void getToken(String refreshToken) throws IOException {

String encoding = "UTF-8";
String params = "client_id=" + clientId + "&refresh_token=" + refreshToken
+ "&grant_type=refresh_token&resource=https%3A%2F%2Fdev.sharepoint.com";
String path = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
byte data = params.getBytes(encoding);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setConnectTimeout(5 * 1000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());

BufferedReader br = null;
if (conn.getResponseCode() != 200) {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
}
System.out.println("Response body : " + br.readLine());
}


}



After getting access token i tried passing the token through postman and getting 401 - Unauthorized



URL used : https://dev.sharepoint.com/sites/_api/Web/GetFolderByServerRelativePath(decodedurl='/sites/Shared%20Documents/Approved')?access_token=eyJ0eXAiOiJKV1QxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxF1V3RWXy1oeFZ0REpKWk00USIsIxxxxxxxxxxxxxxxxxxZnNxZFF1V3RWXy1oeFZ0REpKWk00xxxxxxxxxxxxxxxczovL21hcnJpb3R0ZGV2LnNoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



Response : {"error_description":"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs."}










share|improve this question






















  • Can you post the detailed error trace???
    – klvenky
    Nov 19 at 21:20










  • error_description":"AADSTS700016: Application with identifier 'xxxxxxxxxxxxxxxxxxxxxxx' was not found in the directory 'sharepoint.com'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant
    – Goutham
    Nov 20 at 14:57















up vote
-1
down vote

favorite
1












I used the below code to generate access token and generated access token but not refresh token i'm getting 400 bad request for refresh token.



static String tenantId = "xxxxxxxxxxxxxxxxxxxxxxx";
static String username = "xxxxxxxxxxxxxxxxxxxxxxx";
static String password = "xxxxxxxxx";
static String clientId = "xxxxxxxxxxxxxxxxxxxxxxx";
static String resource = "https://dev.sharepoint.com";
static String userEmail = "xxxxxxxxxxxxxxxxxxxxxx";
static String clientSecret = "xxxxxxxxxxxxxxxxxxx";

public static void main(String args) throws MalformedURLException, IOException {
AuthenticationContext authContext = null;
AuthenticationResult authResult = null;
ExecutorService service = null;

try {
service = Executors.newFixedThreadPool(1);
String url = "https://login.microsoftonline.com/" + tenantId + "/oauth2/authorize";
authContext = new AuthenticationContext(url, false, service);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = authContext.acquireToken(resource, credential, null);

authResult = future.get();
System.out.println("get access token: n" + authResult.getAccessToken());
System.out.println("get refresh token: n" + authResult.getRefreshToken());
} catch (Exception ex) {
ex.printStackTrace();
}
// get access token by refresh token
getToken(authResult.getRefreshToken());
}

public static void getToken(String refreshToken) throws IOException {

String encoding = "UTF-8";
String params = "client_id=" + clientId + "&refresh_token=" + refreshToken
+ "&grant_type=refresh_token&resource=https%3A%2F%2Fdev.sharepoint.com";
String path = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
byte data = params.getBytes(encoding);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setConnectTimeout(5 * 1000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());

BufferedReader br = null;
if (conn.getResponseCode() != 200) {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
}
System.out.println("Response body : " + br.readLine());
}


}



After getting access token i tried passing the token through postman and getting 401 - Unauthorized



URL used : https://dev.sharepoint.com/sites/_api/Web/GetFolderByServerRelativePath(decodedurl='/sites/Shared%20Documents/Approved')?access_token=eyJ0eXAiOiJKV1QxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxF1V3RWXy1oeFZ0REpKWk00USIsIxxxxxxxxxxxxxxxxxxZnNxZFF1V3RWXy1oeFZ0REpKWk00xxxxxxxxxxxxxxxczovL21hcnJpb3R0ZGV2LnNoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



Response : {"error_description":"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs."}










share|improve this question






















  • Can you post the detailed error trace???
    – klvenky
    Nov 19 at 21:20










  • error_description":"AADSTS700016: Application with identifier 'xxxxxxxxxxxxxxxxxxxxxxx' was not found in the directory 'sharepoint.com'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant
    – Goutham
    Nov 20 at 14:57













up vote
-1
down vote

favorite
1









up vote
-1
down vote

favorite
1






1





I used the below code to generate access token and generated access token but not refresh token i'm getting 400 bad request for refresh token.



static String tenantId = "xxxxxxxxxxxxxxxxxxxxxxx";
static String username = "xxxxxxxxxxxxxxxxxxxxxxx";
static String password = "xxxxxxxxx";
static String clientId = "xxxxxxxxxxxxxxxxxxxxxxx";
static String resource = "https://dev.sharepoint.com";
static String userEmail = "xxxxxxxxxxxxxxxxxxxxxx";
static String clientSecret = "xxxxxxxxxxxxxxxxxxx";

public static void main(String args) throws MalformedURLException, IOException {
AuthenticationContext authContext = null;
AuthenticationResult authResult = null;
ExecutorService service = null;

try {
service = Executors.newFixedThreadPool(1);
String url = "https://login.microsoftonline.com/" + tenantId + "/oauth2/authorize";
authContext = new AuthenticationContext(url, false, service);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = authContext.acquireToken(resource, credential, null);

authResult = future.get();
System.out.println("get access token: n" + authResult.getAccessToken());
System.out.println("get refresh token: n" + authResult.getRefreshToken());
} catch (Exception ex) {
ex.printStackTrace();
}
// get access token by refresh token
getToken(authResult.getRefreshToken());
}

public static void getToken(String refreshToken) throws IOException {

String encoding = "UTF-8";
String params = "client_id=" + clientId + "&refresh_token=" + refreshToken
+ "&grant_type=refresh_token&resource=https%3A%2F%2Fdev.sharepoint.com";
String path = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
byte data = params.getBytes(encoding);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setConnectTimeout(5 * 1000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());

BufferedReader br = null;
if (conn.getResponseCode() != 200) {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
}
System.out.println("Response body : " + br.readLine());
}


}



After getting access token i tried passing the token through postman and getting 401 - Unauthorized



URL used : https://dev.sharepoint.com/sites/_api/Web/GetFolderByServerRelativePath(decodedurl='/sites/Shared%20Documents/Approved')?access_token=eyJ0eXAiOiJKV1QxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxF1V3RWXy1oeFZ0REpKWk00USIsIxxxxxxxxxxxxxxxxxxZnNxZFF1V3RWXy1oeFZ0REpKWk00xxxxxxxxxxxxxxxczovL21hcnJpb3R0ZGV2LnNoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



Response : {"error_description":"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs."}










share|improve this question













I used the below code to generate access token and generated access token but not refresh token i'm getting 400 bad request for refresh token.



static String tenantId = "xxxxxxxxxxxxxxxxxxxxxxx";
static String username = "xxxxxxxxxxxxxxxxxxxxxxx";
static String password = "xxxxxxxxx";
static String clientId = "xxxxxxxxxxxxxxxxxxxxxxx";
static String resource = "https://dev.sharepoint.com";
static String userEmail = "xxxxxxxxxxxxxxxxxxxxxx";
static String clientSecret = "xxxxxxxxxxxxxxxxxxx";

public static void main(String args) throws MalformedURLException, IOException {
AuthenticationContext authContext = null;
AuthenticationResult authResult = null;
ExecutorService service = null;

try {
service = Executors.newFixedThreadPool(1);
String url = "https://login.microsoftonline.com/" + tenantId + "/oauth2/authorize";
authContext = new AuthenticationContext(url, false, service);
ClientCredential credential = new ClientCredential(clientId, clientSecret);
Future<AuthenticationResult> future = authContext.acquireToken(resource, credential, null);

authResult = future.get();
System.out.println("get access token: n" + authResult.getAccessToken());
System.out.println("get refresh token: n" + authResult.getRefreshToken());
} catch (Exception ex) {
ex.printStackTrace();
}
// get access token by refresh token
getToken(authResult.getRefreshToken());
}

public static void getToken(String refreshToken) throws IOException {

String encoding = "UTF-8";
String params = "client_id=" + clientId + "&refresh_token=" + refreshToken
+ "&grant_type=refresh_token&resource=https%3A%2F%2Fdev.sharepoint.com";
String path = "https://login.microsoftonline.com/" + tenantId + "/oauth2/token";
byte data = params.getBytes(encoding);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setConnectTimeout(5 * 1000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
System.out.println(conn.getResponseCode());
System.out.println(conn.getResponseMessage());

BufferedReader br = null;
if (conn.getResponseCode() != 200) {
br = new BufferedReader(new InputStreamReader((conn.getErrorStream())));
} else {
br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
}
System.out.println("Response body : " + br.readLine());
}


}



After getting access token i tried passing the token through postman and getting 401 - Unauthorized



URL used : https://dev.sharepoint.com/sites/_api/Web/GetFolderByServerRelativePath(decodedurl='/sites/Shared%20Documents/Approved')?access_token=eyJ0eXAiOiJKV1QxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxF1V3RWXy1oeFZ0REpKWk00USIsIxxxxxxxxxxxxxxxxxxZnNxZFF1V3RWXy1oeFZ0REpKWk00xxxxxxxxxxxxxxxczovL21hcnJpb3R0ZGV2LnNoxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



Response : {"error_description":"The server was unable to process the request due to an internal error. For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs."}







java azure sharepoint adal4j






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 19:54









Goutham

1




1












  • Can you post the detailed error trace???
    – klvenky
    Nov 19 at 21:20










  • error_description":"AADSTS700016: Application with identifier 'xxxxxxxxxxxxxxxxxxxxxxx' was not found in the directory 'sharepoint.com'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant
    – Goutham
    Nov 20 at 14:57


















  • Can you post the detailed error trace???
    – klvenky
    Nov 19 at 21:20










  • error_description":"AADSTS700016: Application with identifier 'xxxxxxxxxxxxxxxxxxxxxxx' was not found in the directory 'sharepoint.com'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant
    – Goutham
    Nov 20 at 14:57
















Can you post the detailed error trace???
– klvenky
Nov 19 at 21:20




Can you post the detailed error trace???
– klvenky
Nov 19 at 21:20












error_description":"AADSTS700016: Application with identifier 'xxxxxxxxxxxxxxxxxxxxxxx' was not found in the directory 'sharepoint.com'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant
– Goutham
Nov 20 at 14:57




error_description":"AADSTS700016: Application with identifier 'xxxxxxxxxxxxxxxxxxxxxxx' was not found in the directory 'sharepoint.com'. This can happen if the application has not been installed by the administrator of the tenant or consented to by any user in the tenant. You may have sent your authentication request to the wrong tenant
– Goutham
Nov 20 at 14:57

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381735%2fable-to-generate-access-token-but-while-testing-it-is-returning-401-unauthorize%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381735%2fable-to-generate-access-token-but-while-testing-it-is-returning-401-unauthorize%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy