访问用户身份属性
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
用户将使用您的社区连接器创建自己的数据源。对于这些数据源,您的连接器可以访问有效用户的身份。根据数据源凭据,有效用户可以是数据源创建者(所有者凭据)或报告查看者(查看者凭据)。
优势
- 您可以代表用户访问 Google 服务和 API。
- 您可以实现自定义访问控制,以确保仅显示相关数据。
- 您可以为有效用户打造量身定制的体验。
实现步骤
访问用户 OAuth 令牌
通过传递用户的 OAuth 令牌,您的连接器可以代表用户访问 Google 服务和 API。如需在连接器中访问有效用户的 OAuth 令牌,请使用 ScriptApp.getOAuthToken()
。请参阅 getOAuthToken 参考。
此令牌将包含连接器授权期间包含的授权范围。
对于大多数连接器,Apps 脚本会通过解析和处理脚本自动检测所需的范围。您可以随时查看连接器使用的范围。您还可以使用网址字符串在清单中明确设置范围。如果您希望代表用户访问 Google 服务或 API,请在清单中包含相关范围。
访问用户电子邮件地址
在代码中,您可以使用 Session.getEffectiveUser().getEmail()
标识当前有效用户。请参阅 getEffectiveUser 参考。添加此代码会自动将 https://www.googleapis.com/auth/userinfo.email
授权范围添加到您的连接器。
示例:使用用户 OAuth 令牌调用 Google API
- Google 健身连接器通过 Google Fit API 提取有效用户的数据。调用该 API 时,它会传递有效用户的 OAuth 令牌。如需了解实现详情,请参阅源代码。
- Firestore 连接器使用 Cloud Resource Manager 获取有效用户的项目列表。该连接器还会传递有效用户的 OAuth 令牌。如需了解实现详情,请参阅源代码。
示例:基于电子邮件地址的 isAdminUser()
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):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)."]]