Skip to content

Commit d40ce24

Browse files
committed
add sample project
1 parent 16fd056 commit d40ce24

File tree

5 files changed

+80
-6
lines changed

5 files changed

+80
-6
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id 'java'
3+
}
4+
5+
group 'org.example'
6+
version '1.0-SNAPSHOT'
7+
8+
repositories {
9+
mavenCentral()
10+
}
11+
12+
dependencies {
13+
testCompile group: 'junit', name: 'junit', version: '4.12'
14+
implementation project(':coreLibrary')
15+
implementation 'com.azure:azure-identity:1.0.8'
16+
implementation 'com.azure:azure-security-keyvault-secrets:4.1.5'
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include 'coreLibrary'
2+
3+
project(':coreLibrary').projectDir = new File("../../")
4+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import com.azure.identity.DeviceCodeCredential;
2+
import com.azure.identity.DeviceCodeCredentialBuilder;
3+
import com.azure.security.keyvault.secrets.SecretClient;
4+
import com.microsoft.graph.authentication.TokenCredentialAuthProvider;
5+
import com.microsoft.graph.exceptions.AuthenticationException;
6+
import com.microsoft.graph.httpcore.HttpClients;
7+
import okhttp3.*;
8+
9+
import java.io.IOException;
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
14+
public class deviceCodeFlowMain {
15+
16+
public static void main(String[] args) throws AuthenticationException {
17+
deviceCodeFlow();
18+
}
19+
20+
public static void deviceCodeFlow() throws AuthenticationException {
21+
22+
List<String> scopes = Arrays.asList("user.ReadBasic.All", "User.Read");
23+
24+
DeviceCodeCredential deviceCodeCred = new DeviceCodeCredentialBuilder()
25+
.clientId("cfb5d84c-c88f-466c-81fb-0fe9fa8da052")
26+
.challengeConsumer(challenge -> {System.out.println(challenge.getMessage());})
27+
.build();
28+
29+
TokenCredentialAuthProvider tokenCredAuthProvider = new TokenCredentialAuthProvider(deviceCodeCred, scopes);
30+
OkHttpClient httpClient = HttpClients.createDefault(tokenCredAuthProvider);
31+
32+
Request request = new Request.Builder().url("https://graph.microsoft.com/v1.0/me/").build();
33+
34+
httpClient.newCall(request).enqueue(new Callback() {
35+
public void onResponse(Call call, Response response) throws IOException {
36+
String responseBody = response.body().string();
37+
System.out.println(responseBody);
38+
}
39+
40+
@Override
41+
public void onFailure(Call call, IOException e) {
42+
e.printStackTrace();
43+
}
44+
});
45+
}
46+
}

src/main/java/com/microsoft/graph/authentication/TokenCredentialAuthProvider.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ public Request authenticateRequest(Request request) {
7676
* @return String representing the retrieved AccessToken
7777
*/
7878
String getAccessToken() {
79-
String[] accessToken = new String[1];
80-
this.tokenCredential.getToken(this.context).doOnError(exception -> exception.printStackTrace())
81-
.subscribe(token -> {
82-
accessToken[0] = token.getToken();
83-
});
84-
return accessToken[0];
79+
String accessToken = null;
80+
try {
81+
AccessToken token = this.tokenCredential.getToken(this.context).block();
82+
accessToken = token.getToken();
83+
} catch (Exception e) {
84+
e.printStackTrace();
85+
}
86+
return accessToken;
8587
}
8688
}

0 commit comments

Comments
 (0)