Angular :如何获得 Google 访问令牌
Angular : how can I get Google access token
我正在尝试使用 Angular 4 获取访问令牌。
当用户使用 Google 登录时,我得到以下数据。
authToken // *
id
email
.
.
.
name
我认为变量 authToken
不是 access token
因为当我尝试使用以下代码( spring 引导)验证它时,
GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new NetHttpTransport(),
JacksonFactory.getDefaultInstance())
.setAudience(Collections.singletonList(
"ID_CLIENT")
.build();
try {
GoogleIdToken idToken = verifier.verify(accessToken);
if (idToken != null) {
Payload payload = idToken.getPayload();
String userId = payload.getSubject();
System.out.println("User ID: " + userId);
String email = payload.getEmail();
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");
System.out.println(accessToken);
return accessToken;
} else {
System.out.println("null");
return "**this is not a valid Token**";
}
} catch (IllegalArgumentException e) {
return "IllegalArgumentException";
}
它return IllegalArgumentException
那么我如何使用 Angular
获得 Google 访问令牌
我正在使用 Angular 4,所以那里没有令牌变量,这与版本 5 和 6 不同,
所以我关注 answer from this
I solved this by going into node_modules/angular4-social-login and making a few changes.
First go into /entities and change the SocialUser model and include "token: string;" like so:
export declare class SocialUser {
provider: string;
id: string;
email: string;
name: string;
photoUrl: string;
token: string;
}
Go to angular4-social-login.umd.js and for every function of the GoogleLoginProvider where the SocialUser object is resolved - find "resolve(user)" - add the line:
user.token = _this.auth2.currentUser.get().getAuthResponse().id_token
Your authService should now return the idToken for the user as user.token.
(3) - for Facebook accessToken, you follow similiar logic. Go to angular4-social-login.umd.js; and for every FacebookLoginProvider function that resolves the user (again search for "resolve(user)"):
Under FB.login(function (response) {
//get the token here
var accessToken = response.authResponse.accessToken
...}
(4) Where the user is being modified, simply add user.token = accessToken before resolving ✨ ✨ ✨
------------------------>the token and authtoken are differents
我正在尝试使用 Angular 4 获取访问令牌。 当用户使用 Google 登录时,我得到以下数据。
authToken // *
id
email
.
.
.
name
我认为变量
authToken
不是access token
因为当我尝试使用以下代码( spring 引导)验证它时,GoogleIdTokenVerifier verifier = new GoogleIdTokenVerifier.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance()) .setAudience(Collections.singletonList( "ID_CLIENT") .build(); try { GoogleIdToken idToken = verifier.verify(accessToken); if (idToken != null) { Payload payload = idToken.getPayload(); String userId = payload.getSubject(); System.out.println("User ID: " + userId); String email = payload.getEmail(); boolean emailVerified = Boolean.valueOf(payload.getEmailVerified()); String name = (String) payload.get("name"); String pictureUrl = (String) payload.get("picture"); String locale = (String) payload.get("locale"); String familyName = (String) payload.get("family_name"); String givenName = (String) payload.get("given_name"); System.out.println(accessToken); return accessToken; } else { System.out.println("null"); return "**this is not a valid Token**"; } } catch (IllegalArgumentException e) { return "IllegalArgumentException"; }
它return
IllegalArgumentException
那么我如何使用 Angular
获得 Google 访问令牌我正在使用 Angular 4,所以那里没有令牌变量,这与版本 5 和 6 不同, 所以我关注 answer from this
I solved this by going into node_modules/angular4-social-login and making a few changes.
First go into /entities and change the SocialUser model and include "token: string;" like so:
export declare class SocialUser {
provider: string;
id: string;
email: string;
name: string;
photoUrl: string;
token: string;
}
Go to angular4-social-login.umd.js and for every function of the GoogleLoginProvider where the SocialUser object is resolved - find "resolve(user)" - add the line:
user.token = _this.auth2.currentUser.get().getAuthResponse().id_token
Your authService should now return the idToken for the user as user.token.
(3) - for Facebook accessToken, you follow similiar logic. Go to angular4-social-login.umd.js; and for every FacebookLoginProvider function that resolves the user (again search for "resolve(user)"):
Under FB.login(function (response) {
//get the token here
var accessToken = response.authResponse.accessToken
...}
(4) Where the user is being modified, simply add user.token = accessToken before resolving ✨ ✨ ✨
------------------------>the token and authtoken are differents