久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

我是否需要 G Suite 帳戶才能使用服務(wù)帳戶冒充用

Do I need G Suite account to make requests impersonating an user with a service account?(我是否需要 G Suite 帳戶才能使用服務(wù)帳戶冒充用戶?)
本文介紹了我是否需要 G Suite 帳戶才能使用服務(wù)帳戶冒充用戶?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在嘗試使用服務(wù)帳戶向 Drive API 發(fā)出模擬用戶的請(qǐng)求,但我收到 TokenResponseException: 401 Unauthorized 異常.

I'm trying to make requests to Drive API impersonating an user using a service account but I'm getting a TokenResponseException: 401 Unauthorized exception.

我已遵循 https://developers.google.com/identity 中的描述/protocols/OAuth2ServiceAccount:

  • 在 https://console.developers.google.com 中,創(chuàng)建了一個(gè) Project > Credentials >服務(wù)帳號(hào) > P12 文件;
  • 啟用 Drive API;
  • 啟用域范圍的委派;
  • 在 https://console.developers.google.com/iam-admin/iam,添加了 Project > Editor 權(quán)限以通過電子郵件發(fā)送 test@gmail.com(示例).
  • In https://console.developers.google.com, created a Project > Credentials > Service account > P12 file;
  • Enabled Drive API;
  • Enabled domain-wide delegation;
  • In https://console.developers.google.com/iam-admin/iam, added Project > Editor permission to email test@gmail.com (example).

然后,在我的代碼中:

File p12 = new File("p12FileFromDevelopersConsole.p12");
HttpTransport transport = GoogleNetHttpTransport.newTrustedTransport();
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

GoogleCredential c = new GoogleCredential.Builder()
    .setTransport(transport)
    .setJsonFactory(jsonFactory)
    .setServiceAccountId("MY_ID@MY-ID.iam.gserviceaccount.com")
    .setServiceAccountUser("test@gmail.com")
    .setServiceAccountScopes(Arrays.asList(DriveScopes.DRIVE, DriveScopes.DRIVE_APPDATA, DriveScopes.DRIVE_FILE, DriveScopes.DRIVE_METADATA_READONLY))
    .setServiceAccountPrivateKeyFromP12File(p12)
    .build();

List<com.google.api.services.drive.model.File> files = drive.files()
    .list()
    .setSpaces("drive")
    .setFields("nextPageToken, files(id, name, webViewLink)")
    .setPageSize(10)
    .execute() // < --- exception is thrown here
    .getFiles();

for(com.google.api.services.drive.model.File f : files) {
    System.out.println(f.getName());
}

堆棧跟蹤是:

com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
    at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384)
    at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
    at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:868)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)

我注意到,如果我注釋掉 .setServiceAccountUser("test@gmail.com") 行,我可以執(zhí)行代碼并在我的 中打印Getting started"System.out.println.

I could note that if I comment out the line .setServiceAccountUser("test@gmail.com"), I am able to execute code and it prints "Getting started" in my System.out.println.

我看過很多關(guān)于這個(gè)問題的帖子,但沒有一個(gè)具體的解決方案.然后,我查看了上面引用的文檔,其中多次提到 G Suite,然后我意識(shí)到這些請(qǐng)求可能只適用于 G Suite 帳戶.我對(duì)嗎?如果沒有,我怎樣才能讓它工作?

I've seen many posts with this problem but none with a concrete solution. Then, I looked at documentation referenced above and it says many times about G Suite and then I realize it could be that these requests only work with a G Suite account. Am I right? If not, how can I make it work?

推薦答案

是的,服務(wù)帳戶的域范圍委派僅適用于 GSuite 用戶,因?yàn)?GSuite 管理員必須授予服務(wù)帳戶域范圍的權(quán)限——請(qǐng)參見此處的步驟 4:https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

Yes, domain-wide delegation for service accounts is only for GSuite users because GSuite admins must grant service accounts domain-wide authority -- see Step 4 here: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority

考慮使用常規(guī) OAuth (https://developers.google.com/identity/protocols/OAuth2WebServer) 以從您的 gmail 帳戶授予對(duì) Web 應(yīng)用程序的訪問權(quán)限.

Consider using regular OAuth (https://developers.google.com/identity/protocols/OAuth2WebServer) to grant access to your web app from your gmail account.

這篇關(guān)于我是否需要 G Suite 帳戶才能使用服務(wù)帳戶冒充用戶?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Upload progress listener not fired (Google drive API)(上傳進(jìn)度偵聽器未觸發(fā)(Google 驅(qū)動(dòng)器 API))
Save file in specific folder with Google Drive SDK(使用 Google Drive SDK 將文件保存在特定文件夾中)
Google Drive Android API - Invalid DriveId and Null ResourceId(Google Drive Android API - 無效的 DriveId 和 Null ResourceId)
Google drive api services account view uploaded files to google drive using java(谷歌驅(qū)動(dòng)api服務(wù)賬戶查看上傳文件到谷歌驅(qū)動(dòng)使用java)
Google Drive service account returns 403 usageLimits(Google Drive 服務(wù)帳號(hào)返回 403 usageLimits)
com.google.api.client.json.jackson.JacksonFactory; missing in Google Drive example(com.google.api.client.json.jackson.JacksonFactory;Google Drive 示例中缺少)
主站蜘蛛池模板: 日韩在线h | 国产98色在线 | 日韩 | 久草在线青青草 | 日本一二区视频 | 亚洲69p | 精产国产伦理一二三区 | 国产精品久久久久久久久免费樱桃 | 日韩视频在线播放 | 国产欧美在线 | 中文字幕av亚洲精品一部二部 | 91在线一区二区 | 狠狠操网站 | 久久久久国色av免费观看性色 | 黄网站在线播放 | 久久久成人免费视频 | 中文字幕电影在线观看 | 亚洲一区精品在线 | 国产一区二区三区免费观看在线 | 国产清纯白嫩初高生视频在线观看 | 欧美日韩国产综合在线 | 日韩视频高清 | 精品国产青草久久久久96 | 久久久久亚洲精品 | 中文字幕在线视频网站 | 午夜影晥 | 欧美一级小视频 | 亚洲成人福利 | 久久久蜜桃 | 国产日韩视频 | 国产成人福利在线观看 | 操操网站| 一区二区三区免费 | 亚洲成人一区 | 亚洲人在线| 久久国产精品首页 | 国产精品色 | 干一干操一操 | 色狠狠一区 | 免费av直接看 | 久久99精品视频 | 91精品国产91久久久久久最新 |