[英]How to get the email of the logged user with oauth2 service account?
我正在嘗試獲取appengine中用戶的電子郵件。 我無法在web.xml文件中使用UserService或。
我將OAuth2與“服務帳戶”配合使用,因此,如果域管理員對其給予較高的評價,則用戶應該是透明的。
這是我正在使用的代碼,但是我找不到任何解決方案。 因為當我通過“ Google Plus Service”請求用戶電子郵件時,返回oauth2 clientID。
GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId("Client_ID_Service_Account@developer.gserviceaccount.com")
.setServiceAccountScopes(Arrays.asList(
"https://www.googleapis.com/auth/plus.me",
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/userinfo.profile"))
.setServiceAccountPrivateKeyFromP12File(
new java.io.File(
"WEB-INF/yyyyyyyyyyyyyyyyyy-privatekey.p12"))
.build();
Plus plus = new Plus(HTTP_TRANSPORT, JSON_FACTORY, credential);
Person profile = plus.people().get("me").execute();
String email=profile.getEmails().get(0);
String state = new BigInteger(130, new SecureRandom()).toString(32);
String send="https://accounts.google.com/o/oauth2/auth?"+
"scope=https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&"+
"state=url%3Dhttps%253A%252F%rrrrrrrrrrrrrrr.appspot.com%26security_token%3D"+state+"&"+
"redirect_uri=https://rrrrrrrrrrrrrrrr.appspot.com/vuelta&"+
"response_type=id_token&"+
"login_hint="+email+"&"+
"client_id=Client_ID_for_web_application.apps.googleusercontent.com";
resp.sendRedirect(send);
我該如何繼續?
提前致謝。
您是代表服務帳戶提出請求的,因此plus.me請求將返回有關服務帳戶的信息。 要代表用戶提出請求,您需要提供一些信息(電子郵件)以標識您感興趣的用戶。否則,Google無法知道您感興趣的用戶。因此,您要問的是除非您已經有電子郵件,否則無法在服務器端進行。
您要執行的是啟動標准(不是服務帳戶)oauth 2認證,並向用戶詢問plus.me范圍。 由於服務帳戶已經具有權限,因此將自動為您授予這些權限。 然后,您可以使用該授權來請求有關用戶的信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.