Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Bạn có thể sử dụng tài khoản dịch vụ trong Trình kết nối cộng đồng để
quản lý quyền truy cập tài nguyên. Một trường hợp sử dụng phổ biến là uỷ quyền truy cập vào
mà người dùng không thể truy cập bằng thông tin đăng nhập của riêng họ.
Bạn có thể hợp nhất thông tin thanh toán để truy cập vào dữ liệu.
Bạn có thể triển khai lớp kiểm soát truy cập của riêng mình trong trình kết nối.
Bạn có thể uỷ quyền truy cập vào dữ liệu hoặc tài nguyên mà thông tin xác thực của người dùng
không có quyền truy cập vào.
Các bước triển khai
Tạo tài khoản dịch vụ cho nền tảng mà bạn đang tìm nạp dữ liệu.
Cấp các quyền cần thiết cho tài khoản dịch vụ để tài khoản dịch vụ có thể truy cập
các tài nguyên cần thiết.
Lưu trữ thông tin đăng nhập của tài khoản dịch vụ trong tập lệnh của trình kết nối
các thuộc tính.
Trong quá trình thực thi trình kết nối, hãy sử dụng thông tin đăng nhập đã lưu trữ để tìm nạp thông tin cần thiết
.
Không bắt buộc: Triển khai logic kiểm soát quyền truy cập để lọc dữ liệu.
Ví dụ: Truy cập vào BigQuery bằng các Dịch vụ nâng cao của Looker Studio và tài khoản dịch vụ
Bạn đang xây dựng một giải pháp trong đó người dùng sẽ xây dựng trang tổng quan từ
Bảng BigQuery. Nếu người dùng sử dụng trình kết nối BigQuery của Looker Studio, họ sẽ
cần quyền đọc bảng BigQuery. Họ cũng sẽ yêu cầu một tài khoản thanh toán
dành cho Google Cloud Platform (GCP). Các bước sau đây minh hoạ cách sử dụng
tài khoản dịch vụ của Google để tổng hợp thông tin thanh toán và uỷ quyền truy cập vào dữ liệu BigQuery.
Đảm bảo tài khoản dịch vụ có thể tạo công việc trong BigQuery và xem dữ liệu
bảng bắt buộc. Xem Kiểm soát quyền truy cập vào BigQuery để biết chi tiết.
Đối với hàm getData, hãy xác thực tài khoản dịch vụ và tạo
mã truy cập. Đặt phạm vi của OAuth2 thành
https://www.googleapis.com/auth/bigquery.readonly.
Trả về mã truy cập cùng các mục cấu hình khác trong phản hồi getData.
Sau đây là ví dụ hoàn chỉnh về mã trình kết nối:
main.js
varcc=DataStudioApp.createCommunityConnector();varscriptProperties=PropertiesService.getScriptProperties();functionisAdminUser(){returntrue;}functiongetAuthType(){varAuthTypes=cc.AuthType;returncc.newAuthTypeResponse().setAuthType(AuthTypes.NONE).build();}functiongetConfig(request){varconfig=cc.getConfig();config.newInfo().setId('generalInfo').setText('This is an example connector to showcase row level security.');returnconfig.build();}functiongetFields(){varfields=cc.getFields();vartypes=cc.FieldType;varaggregations=cc.AggregationType;fields.newDimension().setId('region').setName('Region').setType(types.TEXT);fields.newMetric().setId('sales').setName('Sales').setType(types.NUMBER).setAggregation(aggregations.SUM);fields.newDimension().setId('date').setName('Date').setType(types.YEAR_MONTH_DAY);returnfields;}functiongetSchema(request){return{schema:getFields().build()};}varSERVICE_ACCOUNT_CREDS='SERVICE_ACCOUNT_CREDS';varSERVICE_ACCOUNT_KEY='private_key';varSERVICE_ACCOUNT_EMAIL='client_email';varBILLING_PROJECT_ID='project_id';/** * Copy the entire credentials JSON file from creating a service account in GCP. */functiongetServiceAccountCreds(){returnJSON.parse(scriptProperties.getProperty(SERVICE_ACCOUNT_CREDS));}functiongetOauthService(){varserviceAccountCreds=getServiceAccountCreds();varserviceAccountKey=serviceAccountCreds[SERVICE_ACCOUNT_KEY];varserviceAccountEmail=serviceAccountCreds[SERVICE_ACCOUNT_EMAIL];returnOAuth2.createService('RowLevelSecurity').setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth').setTokenUrl('https://accounts.google.com/o/oauth2/token').setPrivateKey(serviceAccountKey).setIssuer(serviceAccountEmail).setPropertyStore(scriptProperties).setCache(CacheService.getScriptCache()).setScope(['https://www.googleapis.com/auth/bigquery.readonly']);}varBASE_SQL='SELECT d.region, d.sales, d.date '+'FROM `datastudio-solutions.row_level_security.data` d '+'INNER JOIN `datastudio-solutions.row_level_security.access` a '+'ON d.region = a.region '+'where a.email=@email';functiongetData(request){varaccessToken=getOauthService().getAccessToken();varserviceAccountCreds=getServiceAccountCreds();varbillingProjectId=serviceAccountCreds[BILLING_PROJECT_ID];varemail=Session.getEffectiveUser().getEmail();varbqTypes=DataStudioApp.createCommunityConnector().BigQueryParameterType;returncc.newBigQueryConfig().setAccessToken(accessToken).setBillingProjectId(billingProjectId).setUseStandardSql(true).setQuery(BASE_SQL).addQueryParameter('email',bqTypes.STRING,email).build();}
[null,null,["Cập nhật lần gần đây nhất: 2025-07-25 UTC."],[[["\u003cp\u003eCommunity Connectors can utilize service accounts for centralized resource access management, enabling data access delegation beyond user credentials.\u003c/p\u003e\n"],["\u003cp\u003eService accounts offer benefits like consolidated billing, custom access control implementation, and access to otherwise restricted data or resources.\u003c/p\u003e\n"],["\u003cp\u003eImplementing service accounts involves creating a dedicated account, granting necessary permissions, securely storing credentials in script properties, and utilizing these during connector execution.\u003c/p\u003e\n"],["\u003cp\u003eFor enhanced security, avoid storing service account credentials directly in code; instead, leverage connector script properties to safeguard sensitive information.\u003c/p\u003e\n"],["\u003cp\u003eThe provided example demonstrates using a service account with Looker Studio Advanced Services for secure and controlled access to BigQuery data, consolidating billing and delegating access efficiently.\u003c/p\u003e\n"]]],[],null,["# Use a service account\n\nYou can use service accounts in your Community Connectors for centralized\nmanagement of resource access. A common use case would be to delegate access to\ndata that users would not able to access using their own credentials.\n\nReview [Understanding service accounts](https://cloud.google.com/iam/docs/understanding-service-accounts) to familiarize yourself with the topic.\n\nBenefits\n--------\n\n- You can consolidate billing for data access.\n- You can implement your own access control layer in your connector.\n- You can delegate access to data or resources that the user's credentials does not have access to.\n\nImplementation steps\n--------------------\n\n1. Create a service account for the platform from which you are fetching data.\n2. Provide the necessary permissions to the service account so it can access required resources.\n3. Store the service account's credentials in your connector's script properties.\n4. During connector execution, use the stored credentials to fetch required data.\n5. *Optional*: Implement access control logic to filter the data.\n\n| **Caution:** Do not store the service account's credentials in your code. Instead use the connector's script properties to ensure that other users with view access to your code (either via Apps Script or via external code repository) cannot see the credentials.\n\nExample: Accessing BigQuery with Looker Studio Advanced Services and a service account\n--------------------------------------------------------------------------------------\n\nYou are building a solution where your users will build dashboards from a\nBigQuery table. If your users use Looker Studio's BigQuery connector, they will\nneed read access to the BigQuery table. They will also require a billing account\nfor Google Cloud Platform (GCP). The following steps illustrate how to use a\nservice account to consolidate billing and delegate access to the BigQuery data.\n\n1. [Create a service account](https://cloud.google.com/iam/docs/creating-managing-service-accounts#creating_a_service_account) in your desired GCP project.\n2. Ensure the service account can create BigQuery jobs and view the data for the required table. See [BigQuery Access Control](https://cloud.google.com/bigquery/docs/access-control) for details.\n3. [Create a key for the service account](https://cloud.google.com/iam/docs/creating-managing-service-account-keys#creating_service_account_keys) and store the credentials in your connector's [script properties](/apps-script/reference/properties/properties).\n4. Include the [OAuth2 Apps Script](https://github.com/googleworkspace/apps-script-oauth2#setup) library in your Apps Script project.\n5. For your [`getData`](/looker-studio/connector/reference#getdata) function, authenticate the service account and generate an access token. Set the OAuth2 scope to `https://www.googleapis.com/auth/bigquery.readonly`.\n6. Return access token with other configuration items in `getData` response.\n\n#### The following is a complete example of the connector code:\n\nmain.js \n\n```transact-sql\nvar cc = DataStudioApp.createCommunityConnector();\nvar scriptProperties = PropertiesService.getScriptProperties();\n\nfunction isAdminUser() {\n return true;\n}\n\nfunction getAuthType() {\n var AuthTypes = cc.AuthType;\n return cc\n .newAuthTypeResponse()\n .setAuthType(AuthTypes.NONE)\n .build();\n}\n\nfunction getConfig(request) {\n var config = cc.getConfig();\n\n config\n .newInfo()\n .setId('generalInfo')\n .setText('This is an example connector to showcase row level security.');\n\n return config.build();\n}\n\nfunction getFields() {\n var fields = cc.getFields();\n var types = cc.FieldType;\n var aggregations = cc.AggregationType;\n\n fields\n .newDimension()\n .setId('region')\n .setName('Region')\n .setType(types.TEXT);\n\n fields\n .newMetric()\n .setId('sales')\n .setName('Sales')\n .setType(types.NUMBER)\n .setAggregation(aggregations.SUM);\n\n fields\n .newDimension()\n .setId('date')\n .setName('Date')\n .setType(types.YEAR_MONTH_DAY);\n\n return fields;\n}\n\nfunction getSchema(request) {\n return {schema: getFields().build()};\n}\n\nvar SERVICE_ACCOUNT_CREDS = 'SERVICE_ACCOUNT_CREDS';\nvar SERVICE_ACCOUNT_KEY = 'private_key';\nvar SERVICE_ACCOUNT_EMAIL = 'client_email';\nvar BILLING_PROJECT_ID = 'project_id';\n\n/**\n * Copy the entire credentials JSON file from creating a service account in GCP.\n */\nfunction getServiceAccountCreds() {\n return JSON.parse(scriptProperties.getProperty(SERVICE_ACCOUNT_CREDS));\n}\n\nfunction getOauthService() {\n var serviceAccountCreds = getServiceAccountCreds();\n var serviceAccountKey = serviceAccountCreds[SERVICE_ACCOUNT_KEY];\n var serviceAccountEmail = serviceAccountCreds[SERVICE_ACCOUNT_EMAIL];\n\n return OAuth2.createService('RowLevelSecurity')\n .setAuthorizationBaseUrl('https://accounts.google.com/o/oauth2/auth')\n .setTokenUrl('https://accounts.google.com/o/oauth2/token')\n .setPrivateKey(serviceAccountKey)\n .setIssuer(serviceAccountEmail)\n .setPropertyStore(scriptProperties)\n .setCache(CacheService.getScriptCache())\n .setScope(['https://www.googleapis.com/auth/bigquery.readonly']);\n}\n\nvar BASE_SQL =\n 'SELECT d.region, d.sales, d.date ' +\n 'FROM `datastudio-solutions.row_level_security.data` d ' +\n 'INNER JOIN `datastudio-solutions.row_level_security.access` a ' +\n 'ON d.region = a.region ' +\n 'where a.email=@email';\n\nfunction getData(request) {\n var accessToken = getOauthService().getAccessToken();\n var serviceAccountCreds = getServiceAccountCreds();\n var billingProjectId = serviceAccountCreds[BILLING_PROJECT_ID];\n var email = Session.getEffectiveUser().getEmail();\n\n var bqTypes = DataStudioApp.createCommunityConnector().BigQueryParameterType;\n\n return cc\n .newBigQueryConfig()\n .setAccessToken(accessToken)\n .setBillingProjectId(billingProjectId)\n .setUseStandardSql(true)\n .setQuery(BASE_SQL)\n .addQueryParameter('email', bqTypes.STRING, email)\n .build();\n}\n```"]]