存取使用者身分屬性
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
使用者將使用社群連接器建立自己的資料來源。對於這些資料來源,您的連接器可以存取有效使用者身分。視資料來源憑證而定,有效使用者可以是資料來源建立者 (擁有者的憑證),也可以是報表檢視者 (檢視者的憑證)。
優點
- 你可以代表使用者存取 Google 服務和 API。
- 您可以實作自訂存取權控管,確保系統只會顯示相關資料。
- 您可以為有效使用者自訂使用者體驗。
導入步驟
存取使用者 OAuth 權杖
您的連接器只要傳遞使用者的 OAuth 權杖,就能代表使用者存取 Google 服務和 API。如要存取連接器中有效使用者的 OAuth 權杖,請使用 ScriptApp.getOAuthToken()
。請參閱 getOAuthToken 參考資料。這個權杖會包含連接器授權期間包含的授權範圍。
對於大多數連接器,Apps Script 都會透過剖析和處理指令碼來自動偵測所需的範圍。您隨時可以查看連接器使用的範圍。您也可以使用網址字串在資訊清單中明確設定範圍。如要代表使用者存取 Google 服務或 API,請在資訊清單中加入相關範圍。
存取使用者電子郵件地址
在程式碼中,您可以使用 Session.getEffectiveUser().getEmail()
識別目前有效的使用者。請參閱 getEffectiveUser 參考資料。加入這段程式碼後,系統會自動將 https://www.googleapis.com/auth/userinfo.email
授權範圍新增至您的連接器。
範例:使用使用者 OAuth 權杖呼叫 Google API
- Google Fit 連接器會從 Google Fit API 為有效使用者擷取資料。呼叫 API 時,系統會傳遞有效使用者的 OAuth 權杖。如需實作詳情,請參閱原始碼。
- Firestore 連接器會使用 Cloud Resource Manager 取得有效使用者的專案清單。這個連接器也會傳遞有效使用者的 OAuth 權杖。如需實作詳情,請參閱原始碼。
範例:根據電子郵件地址isAdminUser()
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eCommunity Connectors allow you to build data sources that can access the effective user's identity, enabling personalized data retrieval.\u003c/p\u003e\n"],["\u003cp\u003eDepending on the data source credentials, the effective user can be either the data source creator or the report viewer.\u003c/p\u003e\n"],["\u003cp\u003eYour connector can leverage the user's OAuth token to access Google services and APIs on their behalf for custom data integrations.\u003c/p\u003e\n"],["\u003cp\u003eYou can tailor the user experience by implementing custom access control and using the user's email address for identification.\u003c/p\u003e\n"]]],[],null,["# Access user identity properties\n\nUsers will use your Community Connector to create their own data sources. For\nthese data sources, your connector can access the effective user's identity.\nDepending on the [Data source credentials](https://support.google.com/looker-studio/answer/6371135), the effective user can be the data\nsource creator (Owner's credentials) or the report viewer (Viewer's\ncredentials).\n\nBenefits\n--------\n\n- You can access Google services and APIs on behalf of the user.\n- You can implement custom access control to ensure only relevant data is being displayed.\n- You can customize the user experience for the effective user.\n\nImplementation steps\n--------------------\n\n### Accessing the user OAuth token\n\nYour connector can access Google services and APIs on behalf of the user by\npassing the user's OAuth token. To access the effective user's OAuth token in\nyour connector, use `ScriptApp.getOAuthToken()`. See [getOAuthToken reference](/apps-script/reference/script/script-app#getoauthtoken).\nThis token will include the [Authorization scopes](/apps-script/concepts/scopes) included during the connector\nauthorization.\n\nFor most connectors, Apps Script automatically detects what scopes are needed by\nparsing and processing the script. You can [view the scopes](/apps-script/concepts/scopes#viewing_scopes) a connector uses at\nany time. You can also [set scopes explicitly](/apps-script/concepts/scopes#setting_explicit_scopes) in your [manifest](/apps-script/concepts/manifests) using URL\nstrings. If you want to access a Google service or API on behalf of the user,\ninclude that relevant scope in the manifest.\n\n### Accessing the user email address\n\nIn your code, you can identify the current effective user with\n`Session.getEffectiveUser().getEmail()`. See [getEffectiveUser reference](/apps-script/reference/base/session#getEffectiveUser).\nAdding this code will automatically add the\n`https://www.googleapis.com/auth/userinfo.email` authorization scope to your\nconnector.\n\nExample: Calling Google APIs with user OAuth token\n--------------------------------------------------\n\n- The [Google Fit connector](https://lookerstudio.google.com/datasources/create?connectorId=AKfycbwFrL1OLXR3wqfnxowGFF3w3D4eq-54vAwjxDWcWRjiZMxX7jhII_1Ddx6y76GDHrxIew) fetches data from the Google Fit API for the effective user. While calling the API, it passes the effective user's OAuth token. See [the source code](https://github.com/googledatastudio/community-connectors/blob/master/google-fit/src/GoogleFit.js#L245) for implementation details.\n- The [Firestore connector](https://github.com/googledatastudio/community-connectors/tree/master/firestore) uses the Cloud Resource Manager to get a list of projects for the effective user. This connector also passes the effective user's OAuth token. See [the source code](https://github.com/googledatastudio/community-connectors/blob/master/firestore/src/GoogleCloud.gs#L30) for implementation details.\n\nExample: `isAdminUser()` based on email address\n-----------------------------------------------\n\n- The [Chrome UX Connector](http://g.co/chromeuxdash) maintains a list of admin users. It uses `getEffectiveUser()` and compares the effective user against the list to determine whether the effective user is an admin or not. See [implementation of `getEffectiveUser`](https://github.com/googledatastudio/community-connectors/blob/master/chrome-ux-report/src/Code.js#L581)."]]