커뮤니티 커넥터는 다음과 같은 인증 방식을 지원합니다.
- OAuth 2.0
- 경로/사용자 이름/비밀번호
- 경로/키
- 사용자 이름/비밀번호
- 사용자 이름/토큰
- 키
- 없음
사용 중인 방법에 따라 커넥터에 추가 함수를 제공해야 합니다.
다음 표는 커넥터의 인증 유형에 따라 정의해야 하는 함수를 보여줍니다.
OAUTH2 | PATH_USER_PASS PATH_KEY USER_PASS USER_TOKEN KEY |
없음 | |
---|---|---|---|
getAuthType() |
필수 | 필수 | 필수 |
resetAuth() |
필수 | 필수 | |
isAuthValid() |
필수 | 필수 | |
authCallback() |
필수 | ||
get3PAuthorizationUrls() |
필수 | ||
setCredentials() |
필수 |
getAuthType()
이 함수는 커넥터의 인증 유형을 반환해야 합니다.
OAUTH2
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
USER_TOKEN
KEY
없음
resetAuth()
이 함수는 서드 파티 서비스에서 사용자에 대해 저장된 모든 사용자 인증 정보를 삭제합니다.
OAUTH2
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
USER_TOKEN
KEY
isAuthValid()
이 함수는 서드 파티 서비스에 대한 인증이 유효한지 확인하기 위해 호출됩니다. 인증이 유효하면 getData()
및 getSchema()
호출이 승인되지 않은 액세스로 인해 실패하지 않습니다. 인증이 유효하지 않으면 사용자는 승인 흐름을 시작하라는 알림을 받을 수 있습니다.
OAUTH2
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
USER_TOKEN
KEY
OAUTH2
Apps Script 라이브러리용 OAuth2 추가 및 설정
Apps Script용 OAuth2 라이브러리의 설정 안내에 따라 커넥터 프로젝트에 추가합니다. 그런 다음 사용 가이드의 1단계에 따라 커넥터 프로젝트에서 OAuth2 서비스를 만듭니다. OAuth2 서비스의 함수 이름은 유효할 수 있지만 코드에서 OAuth2 서비스를 참조할 때는 동일한 이름을 사용해야 합니다.
예를 들어 exampleService
라는 OAuth2 서비스는 다음과 같습니다.
authCallback()
이 함수는 OAuth 2.0 흐름을 완료하기 위해 호출됩니다. 서드 파티 인증 서비스의 콜백 응답은 인수로 제공되며 이 함수로 처리되어야 합니다.
다음은 Apps Script용 OAuth2 라이브러리를 사용하여 OAuth 2.0 콜백을 처리하는 예입니다.
get3PAuthorizationUrls()
이 함수는 서드 파티 서비스의 인증 흐름을 시작하는 데 필요한 URL을 가져오기 위해 호출됩니다. isAuthValid
가 false
를 반환하면 반환된 URL이 사용자에게 버튼 또는 링크로 표시되므로 사용자가 서드 파티 서비스에 대한 액세스를 승인할 수 있습니다. get3PAuthorizationUrls() 참조를 확인하세요.
다음은 Apps Script 라이브러리용 OAuth2를 사용하여 승인 URL을 반환하는 예입니다.
USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
및 PATH_KEY
다음은 USER_PASS
, USER_TOKEN
, KEY
, PATH_USER_PASS
, PATH_KEY
인증 흐름에만 필요합니다.
setCredentials()
setCredentials
는 사용자가 커뮤니티 커넥터 구성 페이지에 사용자 인증 정보 정보를 입력한 후에 호출됩니다. 속성 서비스에서 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'
};
}