การตรวจสอบสิทธิ์

ปลั๊กอินจากชุมชนสำหรับใช้ลิงก์ข้อมูลรองรับวิธีการตรวจสอบสิทธิ์ต่อไปนี้

  • OAuth 2.0
  • เส้นทาง/ชื่อผู้ใช้/รหัสผ่าน
  • เส้นทาง/คีย์
  • ชื่อผู้ใช้/รหัสผ่าน
  • ชื่อผู้ใช้/โทเค็น
  • คีย์
  • ไม่มี

คุณต้องระบุฟังก์ชันเพิ่มเติมในเครื่องมือเชื่อมต่อ ทั้งนี้ขึ้นอยู่กับวิธีที่คุณใช้

ตารางต่อไปนี้ระบุฟังก์ชันที่คุณต้องกำหนด ทั้งนี้ขึ้นอยู่กับประเภทการตรวจสอบสิทธิ์ของเครื่องมือเชื่อมต่อของคุณ

OAUTH2 PATH_USER_PASS
PATH_KEY
USER_PASS
USER_TOKEN
KEY
ไม่มี
getAuthType() จำเป็น จำเป็น จำเป็น
resetAuth() จำเป็น จำเป็น
isAuthValid() จำเป็น จำเป็น
authCallback() จำเป็น
get3PAuthorizationUrls() จำเป็น
setCredentials() จำเป็น
เพื่อให้แน่ใจว่าข้อมูลเข้าสู่ระบบจะเก็บเป็นความลับ

getAuthType()

ฟังก์ชันนี้ควรแสดงประเภทการตรวจสอบสิทธิ์สำหรับเครื่องมือเชื่อมต่อ

OAUTH2

data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */
function getAuthType() {
  var cc = DataStudioApp.createCommunityConnector();
  return cc.newAuthTypeResponse()
      .setAuthType(cc.AuthType.OAUTH2)
      .build();
}

PATH_USER_PASS

/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */
function getAuthType() {
  var cc = DataStudioApp.createCommunityConnector();
  return cc.newAuthTypeResponse()
    .setAuthType(cc.AuthType.PATH_USER_PASS)
    .setHelpUrl('https://www.example.org/connector-auth-help')
    .build();
}

PATH_KEY

/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */
function getAuthType() {
  var cc = DataStudioApp.createCommunityConnector();
  return cc.newAuthTypeResponse()
    .setAuthType(cc.AuthType.PATH_KEY)
    .setHelpUrl('https://www.example.org/connector-auth-help')
    .build();
}

USER_PASS

data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */
function getAuthType() {
  var cc = DataStudioApp.createCommunityConnector();
  return cc.newAuthTypeResponse()
      .setAuthType(cc.AuthType.USER_PASS)
      .setHelpUrl('https://www.example.org/connector-auth-help')
      .build();
}

USER_TOKEN

data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */
function getAuthType() {
  var cc = DataStudioApp.createCommunityConnector();
  return cc.newAuthTypeResponse()
      .setAuthType(cc.AuthType.USER_TOKEN)
      .setHelpUrl('https://www.example.org/connector-auth-help')
      .build();
}

KEY

data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */
function getAuthType() {
  var cc = DataStudioApp.createCommunityConnector();
  return cc.newAuthTypeResponse()
      .setAuthType(cc.AuthType.KEY)
      .setHelpUrl('https://www.example.org/connector-auth-help')
      .build();
}

ไม่มี

data-studio/auth.gs
/**
 * Returns the Auth Type of this connector.
 * @return {object} The Auth type.
 */
function getAuthType() {
  var cc = DataStudioApp.createCommunityConnector();
  return cc.newAuthTypeResponse()
      .setAuthType(cc.AuthType.NONE)
      .build();
}

resetAuth()

ฟังก์ชันนี้จะล้างข้อมูลเข้าสู่ระบบทั้งหมดที่จัดเก็บไว้สำหรับผู้ใช้สำหรับบริการของบุคคลที่สาม

OAUTH2

data-studio/auth.gs
/**
 * Resets the auth service.
 */
function resetAuth() {
  getOAuthService().reset();
}

PATH_USER_PASS

/**
 * Resets the auth service.
 */
function resetAuth() {
  var userProperties = PropertiesService.getUserProperties();
  userProperties.deleteProperty('dscc.path');
  userProperties.deleteProperty('dscc.username');
  userProperties.deleteProperty('dscc.password');
}

PATH_KEY

/**
 * Resets the auth service.
 */
function resetAuth() {
  var userProperties = PropertiesService.getUserProperties();
  userProperties.deleteProperty('dscc.path');
  userProperties.deleteProperty('dscc.key');
}

USER_PASS

data-studio/auth.gs
/**
 * Resets the auth service.
 */
function resetAuth() {
  var userProperties = PropertiesService.getUserProperties();
  userProperties.deleteProperty('dscc.username');
  userProperties.deleteProperty('dscc.password');
}

USER_TOKEN

data-studio/auth.gs
/**
 * Resets the auth service.
 */
function resetAuth() {
  var userTokenProperties = PropertiesService.getUserProperties();
  userTokenProperties.deleteProperty('dscc.username');
  userTokenProperties.deleteProperty('dscc.password');
}

KEY

data-studio/auth.gs
/**
 * Resets the auth service.
 */
function resetAuth() {
  var userProperties = PropertiesService.getUserProperties();
  userProperties.deleteProperty('dscc.key');
}

isAuthValid()

เราจะเรียกใช้ฟังก์ชันนี้เพื่อระบุว่าการตรวจสอบสิทธิ์สำหรับบริการของบุคคลที่สามถูกต้องหรือไม่ หากการตรวจสอบสิทธิ์ถูกต้อง ก็คาดว่าการเรียกใช้ไปยัง getData() และ getSchema() จะไม่ล้มเหลวเนื่องจากการเข้าถึงที่ไม่ได้รับอนุญาต หากการตรวจสอบสิทธิ์ไม่ถูกต้อง ผู้ใช้อาจได้รับการแจ้งเตือนให้เริ่มขั้นตอนการให้สิทธิ์

OAUTH2

data-studio/auth.gs
/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */
function isAuthValid() {
  return getOAuthService().hasAccess();
}

PATH_USER_PASS

/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */
function isAuthValid() {
  var userProperties = PropertiesService.getUserProperties();
  var path = userProperties.getProperty('dscc.path');
  var userName = userProperties.getProperty('dscc.username');
  var password = userProperties.getProperty('dscc.password');
  // This assumes you have a validateCredentials function that
  // can validate if the path, userName and password are correct.
  return validateCredentials(path, userName, password);
}

PATH_KEY

/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */
function isAuthValid() {
  var userProperties = PropertiesService.getUserProperties();
  var path = userProperties.getProperty('dscc.path');
  var key = userProperties.getProperty('dscc.key');
  // This assumes you have a validateCredentials function that
  // can validate if the path and key are correct.
  return validateCredentials(path, key);
}

USER_PASS

data-studio/auth.gs
/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */
function isAuthValid() {
  var userProperties = PropertiesService.getUserProperties();
  var userName = userProperties.getProperty('dscc.username');
  var password = userProperties.getProperty('dscc.password');
  // This assumes you have a validateCredentials function that
  // can validate if the userName and password are correct.
  return validateCredentials(userName, password);
}

USER_TOKEN

data-studio/auth.gs
/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */
function isAuthValid() {
  var userProperties = PropertiesService.getUserProperties();
  var userName = userProperties.getProperty('dscc.username');
  var token = userProperties.getProperty('dscc.token');
  // This assumes you have a validateCredentials function that
  // can validate if the userName and token are correct.
  return validateCredentials(userName, token);
}

KEY

data-studio/auth.gs
/**
 * Returns true if the auth service has access.
 * @return {boolean} True if the auth service has access.
 */
function isAuthValid() {
  var userProperties = PropertiesService.getUserProperties();
  var key = userProperties.getProperty('dscc.key');
  // This assumes you have a validateKey function that can validate
  // if the key is valid.
  return validateKey(key);
}

OAUTH2

เพิ่มและตั้งค่า OAuth2 สำหรับ Apps Script Library

ทำตามวิธีการตั้งค่าสำหรับไลบรารี OAuth2 สำหรับ Apps Script เพื่อเพิ่มไลบรารีนี้ในโปรเจ็กต์เครื่องมือเชื่อมต่อ จากนั้นทำตามขั้นตอนแรกในคู่มือการใช้งานเพื่อสร้างบริการ OAuth2 ในโปรเจ็กต์เครื่องมือเชื่อมต่อ บริการ OAuth2 จะมีชื่อฟังก์ชันที่ถูกต้องได้ แต่โปรดตรวจสอบว่าใช้ชื่อเดียวกันขณะอ้างอิงถึงบริการ OAuth2 ในโค้ด

เช่น บริการ OAuth2 ชื่อ exampleService

data-studio/auth.gs
/**
 * Returns the configured OAuth Service.
 * @return {Service} The OAuth Service
 */
function getOAuthService() {
  return OAuth2.createService('exampleService')
      .setAuthorizationBaseUrl('...')
      .setTokenUrl('...')
      .setClientId('...')
      .setClientSecret('...')
      .setPropertyStore(PropertiesService.getUserProperties())
      .setCallbackFunction('authCallback')
      .setScope('...');
};

authCallback()

ฟังก์ชันนี้เรียกว่าเพื่อให้ขั้นตอน OAuth 2.0 ดำเนินการให้เสร็จสมบูรณ์ การตอบสนองการเรียกกลับจากบริการการตรวจสอบสิทธิ์ของบุคคลที่สามจะมีการระบุเป็นอาร์กิวเมนต์และควรได้รับการจัดการโดยฟังก์ชันนี้

ตัวอย่างการจัดการโค้ดเรียกกลับของ OAuth 2.0 โดยใช้ไลบรารี OAuth2 สำหรับ Apps Script

data-studio/auth.gs
/**
 * The OAuth callback.
 * @param {object} request The request data received from the OAuth flow.
 * @return {HtmlOutput} The HTML output to show to the user.
 */
function authCallback(request) {
  var authorized = getOAuthService().handleCallback(request);
  if (authorized) {
    return HtmlService.createHtmlOutput('Success! You can close this tab.');
  } else {
    return HtmlService.createHtmlOutput('Denied. You can close this tab');
  };
};

get3PAuthorizationUrls()

โดยจะเรียกฟังก์ชันนี้เพื่อรับ URL ที่จำเป็นในการเริ่มต้นขั้นตอนการตรวจสอบสิทธิ์สำหรับบริการของบุคคลที่สาม หาก isAuthValid แสดงผล false ระบบจะแสดง URL ที่ส่งคืนแก่ผู้ใช้เป็นปุ่มหรือลิงก์เพื่อให้ผู้ใช้ให้สิทธิ์เข้าถึงบริการของบุคคลที่สามได้ ดูข้อมูลอ้างอิงสำหรับ get3PAuthorizationUrls()

ตัวอย่างการแสดง URL การให้สิทธิ์โดยใช้ OAuth2 สำหรับไลบรารี Apps Script

data-studio/auth.gs
/**
 * Gets the 3P authorization URL.
 * @return {string} The authorization URL.
 * @see https://developers.google.com/apps-script/reference/script/authorization-info
 */
function get3PAuthorizationUrls() {
  return getOAuthService().getAuthorizationUrl();
}

USER_PASS USER_TOKEN KEY PATH_USER_PASS และPATH_KEY

ค่าต่อไปนี้จำเป็นสำหรับขั้นตอนการตรวจสอบสิทธิ์ USER_PASS, USER_TOKEN, KEY, PATH_USER_PASS และ PATH_KEY เท่านั้น

setCredentials()

ระบบจะเรียกใช้ setCredentials หลังจากที่ผู้ใช้ป้อนข้อมูลเข้าสู่ระบบในหน้าการกำหนดค่าปลั๊กอินจากชุมชนสำหรับใช้ลิงก์ข้อมูล คุณควรใช้ Properties Service เพื่อบันทึกข้อมูลเข้าสู่ระบบตามผู้ใช้แต่ละรายโดยใช้ UserProperties

PATH_USER_PASS

/**
 * Sets the credentials.
 * @param {Request} request The set credentials request.
 * @return {object} An object with an errorCode.
 */
function setCredentials(request) {
  var creds = request.pathUserPass;
  var path = creds.path;
  var username = creds.username;
  var password = creds.password;

  // Optional
  // Check if the provided path, username and password are valid through
  // a call to your service. You would have to have a `checkForValidCreds`
  // function defined for this to work.
  var validCreds = checkForValidCreds(path, username, password);
  if (!validCreds) {
    return {
      errorCode: 'INVALID_CREDENTIALS'
    };
  }
  var userProperties = PropertiesService.getUserProperties();
  userProperties.setProperty('dscc.path', path);
  userProperties.setProperty('dscc.username', username);
  userProperties.setProperty('dscc.password', password);
  return {
    errorCode: 'NONE'
  };
}

PATH_KEY

/**
 * Sets the credentials.
 * @param {Request} request The set credentials request.
 * @return {object} An object with an errorCode.
 */
function setCredentials(request) {
  var creds = request.pathKey;
  var path = creds.path;
  var key = creds.key;

  // Optional
  // Check if the provided path and key are valid through
  // a call to your service. You would have to have a `checkForValidCreds`
  // function defined for this to work.
  var validCreds = checkForValidCreds(path, key);
  if (!validCreds) {
    return {
      errorCode: 'INVALID_CREDENTIALS'
    };
  }
  var userProperties = PropertiesService.getUserProperties();
  userProperties.setProperty('dscc.path', path);
  userProperties.setProperty('dscc.key', key);
  return {
    errorCode: 'NONE'
  };
}

USER_PASS

data-studio/auth.gs
/**
 * Sets the credentials.
 * @param {Request} request The set credentials request.
 * @return {object} An object with an errorCode.
 */
function setCredentials(request) {
  var creds = request.userPass;
  var username = creds.username;
  var password = creds.password;

  // Optional
  // Check if the provided username and password are valid through a
  // call to your service. You would have to have a `checkForValidCreds`
  // function defined for this to work.
  var validCreds = checkForValidCreds(username, password);
  if (!validCreds) {
    return {
      errorCode: 'INVALID_CREDENTIALS'
    };
  }
  var userProperties = PropertiesService.getUserProperties();
  userProperties.setProperty('dscc.username', username);
  userProperties.setProperty('dscc.password', password);
  return {
    errorCode: 'NONE'
  };
}

USER_TOKEN

data-studio/auth.gs
/**
 * Sets the credentials.
 * @param {Request} request The set credentials request.
 * @return {object} An object with an errorCode.
 */
function setCredentials(request) {
  var creds = request.userToken;
  var username = creds.username;
  var token = creds.token;

  // Optional
  // Check if the provided username and token are valid through a
  // call to your service. You would have to have a `checkForValidCreds`
  // function defined for this to work.
  var validCreds = checkForValidCreds(username, token);
  if (!validCreds) {
    return {
      errorCode: 'INVALID_CREDENTIALS'
    };
  }
  var userProperties = PropertiesService.getUserProperties();
  userProperties.setProperty('dscc.username', username);
  userProperties.setProperty('dscc.token', token);
  return {
    errorCode: 'NONE'
  };
}

KEY

data-studio/auth.gs
/**
 * Sets the credentials.
 * @param {Request} request The set credentials request.
 * @return {object} An object with an errorCode.
 */
function setCredentials(request) {
  var key = request.key;

  // Optional
  // Check if the provided key is valid through a call to your service.
  // You would have to have a `checkForValidKey` function defined for
  // this to work.
  var validKey = checkForValidKey(key);
  if (!validKey) {
    return {
      errorCode: 'INVALID_CREDENTIALS'
    };
  }
  var userProperties = PropertiesService.getUserProperties();
  userProperties.setProperty('dscc.key', key);
  return {
    errorCode: 'NONE'
  };
}