Google 데이터 프로토콜 클라이언트 라이브러리의 OAuth

경고: 이 페이지는 Google의 이전 API인 Google Data API에 관한 내용이며, Google Data API 디렉터리에 나열된 API에만 관련이 있습니다. 이 API 중 상당수는 최신 API로 대체되었습니다. 특정 새 API에 관한 자세한 내용은 새 API의 문서를 참고하세요. 최신 API로 요청을 승인하는 방법에 대한 자세한 내용은 Google 계정 인증 및 승인을 참고하세요.

이 문서에서는 Google 데이터 API 클라이언트 라이브러리를 사용하여 Google의 웹 애플리케이션용 OAuth 인증에 연결하는 방법을 설명합니다.

OAuth 인터페이스를 사용하면 웹 기반 애플리케이션이 사용자를 대신하여 Google 서비스에 액세스할 수 있습니다. 높은 수준의 보안을 유지하기 위해 OAuth를 사용하면 애플리케이션이 사용자의 계정 로그인 정보를 처리하지 않고도 액세스 토큰을 가져올 수 있습니다.

Google Data API 클라이언트 라이브러리는 웹 애플리케이션에서 OAuth를 사용하는 데 도움이 되는 메서드를 제공합니다. 특히 요청 토큰을 획득하고, 요청 토큰을 승인하고, 승인된 요청 토큰을 액세스 토큰으로 교환하는 메서드가 있습니다. 또한 라이브러리는 Google 데이터 서비스에 요청할 때 필요한 서명 알고리즘을 처리합니다.

잠재고객

이 문서는 Google Data API 클라이언트 라이브러리를 사용하여 사용자를 대신하여 웹 기반 애플리케이션이 Google 서비스에 액세스하도록 하려는 프로그래머를 대상으로 합니다.

이 문서에서는 사용자가 OAuth 인터페이스와 웹 애플리케이션에 OAuth를 통합하는 일반적인 프로세스에 익숙하다고 가정합니다. OAuth 프로토콜에 관한 전체 설명은 웹 애플리케이션용 OAuth 인증 또는 oauth.net의 공식 사양을 참고하세요.

클라이언트 라이브러리 없이 3-legged OAuth 및 Google Data API 사용

OAuth를 승인 방법으로 사용하여 웹 애플리케이션이 Google 데이터 서비스와 상호작용하도록 하려면 웹 애플리케이션용 OAuth 인증을 참고하세요. 원하지 않는 경우 Google Data API 클라이언트 라이브러리를 사용하지 않아도 됩니다.

다음은 애플리케이션이 OAuth를 사용하여 사용자를 인증하는 방법의 개요입니다.

  1. 애플리케이션이 OAuthRequestToken 엔드포인트에서 초기 OAuth 요청 토큰을 가져오기 위해 서명된 요청을 합니다.
  2. 애플리케이션은 요청 토큰을 승인하기 위해 사용자를 적절한 OAuthAuthorizeToken URL로 리디렉션합니다.
  3. 액세스 권한을 부여하면 사용자가 애플리케이션 (oauth_callback URL)으로 다시 리디렉션됩니다.
  4. 애플리케이션이 OAuthGetAccessToken 엔드포인트를 사용하여 승인된 요청 토큰을 액세스 토큰으로 업그레이드하는 서명된 요청을 전송합니다.

Google Data API 클라이언트 라이브러리는 다양한 세부정보를 처리하여 이 승인 프로세스를 간소화합니다. 이 문서에서는 방법을 설명합니다.

웹 애플리케이션 등록

OAuth에서는 모든 API 호출이 디지털 서명되어야 합니다. Google은 HMAC-SHA1RSA-SHA1 서명 방법을 지원합니다. 요청에 서명하려면 애플리케이션을 먼저 Google에 등록해야 합니다. 등록이 완료되면 Google에서 고객 키 (및 HMAC-SHA1와 함께 사용할 보안 비밀)와 공개 인증서를 업로드할 위치를 제공합니다.

1. 도메인 등록

웹 기반 애플리케이션 등록에 설명된 단계를 따르세요.

2. 비공개 키 / 공개 인증서 쌍 만들기 (선택사항)

RSA-SHA1oauth_signature_method로 사용하려면 자체 서명 RSA 비공개 키와 공개 인증서 쌍을 만들어야 합니다. 이를 수행하는 방법의 예는 자체 서명 비공개 키 및 공개 인증서 생성(아래)을 참고하세요.

3단계 OAuth 및 Google Data API 사용: 클라이언트 라이브러리 예

다음 섹션에서는 Google Data API 클라이언트 라이브러리 메서드를 사용하여 OAuth 문서의 'OAuth 사용' 섹션에 설명된 단계를 따르는 예를 보여줍니다. 이 문서의 모든 예시에서는 애플리케이션 호스트 도메인이 example.com이라고 가정합니다.

데이터 액세스 범위 결정

각 Google 서비스는 토큰의 사용자 데이터 액세스를 결정하는 scope 값을 정의합니다. 사용 가능한 범위 값은 Google 데이터 FAQ에 나열되어 있습니다. 예를 들어 Documents List API를 사용하려면 FAQ에 나와 있는 대로 scopehttps://docs.google.com/feeds/로 설정합니다.

참고: 필요한 액세스를 허용하는 가장 좁은 URL로 scope 값을 설정합니다. 이렇게 하면 실수로 개인 정보를 획득하고 유출할 가능성이 줄어듭니다. 예를 들어 현재 사용자의 비공개 문서 목록 피드에 액세스하려면 모든 문서 목록 피드에 액세스할 수 있는 https://docs.google.com/feeds/와 같은 더 넓은 범위 대신 https://docs.google.com/feeds/default/private/full 범위를 사용합니다.

범위가 여러 개인 토큰

여러 Google 데이터 API에 액세스하는 토큰을 만들려면 각 범위를 공백 문자로 구분하세요. 아래 예에서는 사용자의 Google 문서 및 Google Calendar 데이터에 모두 액세스할 수 있는 토큰을 만듭니다.

scope=https://www.google.com/calendar/feeds/ https://docs.google.com/feeds/
URL 인코딩

콜론, 슬래시, 공백 등 URL에 표시되는 비ASCII 문자는 HTTP를 통해 전송되려면 URL로 인코딩되어야 합니다. Google Data API 클라이언트 라이브러리는 매개변수를 자동으로 URL 인코딩하므로 매개변수에 값을 할당할 때 URL 인코딩되지 않은 문자열을 사용하면 됩니다. 예를 들어 코드에서 다음과 같이 할당할 수 있습니다.

scope=https://www.google.com/calendar/feeds/ https://docs.google.com/feeds/

클라이언트 라이브러리를 호출하면 scope 매개변수가 자동으로 다음 값으로 URL 인코딩됩니다.
https%3a%2f%2fwww.google.com%2fcalendar%2ffeeds%2f+https%3a%2f%2fdocs.google.com%2ffeeds%2f

요청 토큰 가져오기

자바

HMAC-SHA1의 경우 승인 페이지에서 돌아오는 OAuth 토큰 객체를 만들기 위해 응답에서 획득한 토큰 보안 비밀을 유지하는 방법이 필요합니다. 이렇게 하려면 세션 변수나 쿠키를 설정하세요.

import com.google.gdata.client.docs.*;
import com.google.gdata.client.authn.oauth.*;

String CONSUMER_KEY = "example.com";
String CONSUMER_SECRET = "abc123doremi";

GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
oauthParameters.setScope("https://docs.google.com/feeds/");
oauthParameters.setOAuthCallback("http://www.example.com/UpgradeToken.jsp");

GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer());
oauthHelper.getUnauthorizedRequestToken(oauthParameters);

RSA-SHA1를 사용하면 oauth_token_secret이 사용되지 않으므로 토큰 보안 비밀을 유지할 필요가 없습니다.

import com.google.gdata.client.docs.*;
import com.google.gdata.client.authn.oauth.*;

String CONSUMER_KEY = "example.com";

GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
oauthParameters.setScope("https://docs.google.com/feeds/");
oauthParameters.setOAuthCallback("http://www.example.com/UpgradeToken.jsp");

PrivateKey privKey = getPrivateKey("/path/to/your/rsakey.pk8");

GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthRsaSha1Signer(privKey));
oauthHelper.getUnauthorizedRequestToken(oauthParameters);

...

public static PrivateKey getPrivateKey(String privKeyFileName) {
  File privKeyFile = new File(privKeyFileName);
  FileInputStream fis = new FileInputStream(privKeyFile);
  DataInputStream dis  = new DataInputStream(fis);

  byte[] privKeyBytes = new byte[(int) privKeyFile.length()];
  dis.read(privKeyBytes);
  dis.close();
  fis.close();

  String BEGIN = "-----BEGIN PRIVATE KEY-----";
  String END = "-----END PRIVATE KEY-----";
  String str = new String(privKeyBytes);
  if (str.contains(BEGIN) && str.contains(END)) {
    str = str.substring(BEGIN.length(), str.lastIndexOf(END));
  }

  KeyFactory fac = KeyFactory.getInstance("RSA");
  EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Base64.decode(str));
  return fac.generatePrivate(privKeySpec);
}

PHP

HMAC-SHA1를 서명 방법으로 사용하는 경우:

require_once 'Zend/Oauth/Consumer.php';

session_start();

$CONSUMER_KEY = 'example.com';
$CONSUMER_SECRET = 'abc123doremi';

// Multi-scoped token.
$SCOPES = array(
  'https://docs.google.com/feeds/',
  'https://spreadsheets.google.com/feeds/'
);

$oauthOptions = array(
  'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
  'version' => '1.0',
  'consumerKey' => $CONSUMER_KEY,
  'consumerSecret' => $CONSUMER_SECRET,
  'signatureMethod' => 'HMAC-SHA1',
  'callbackUrl' => 'http://myapp.example.com/access_token.php',
  'requestTokenUrl' => 'https://www.google.com/accounts/OAuthGetRequestToken',
  'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken',
  'accessTokenUrl' => 'https://www.google.com/accounts/OAuthGetAccessToken'
);

$consumer = new Zend_Oauth_Consumer($oauthOptions);

// When using HMAC-SHA1, you need to persist the request token in some way.
// This is because you'll need the request token's token secret when upgrading
// to an access token later on. The example below saves the token object as a session variable.
if (!isset($_SESSION['ACCESS_TOKEN'])) {
  $_SESSION['REQUEST_TOKEN'] = serialize($consumer->getRequestToken(array('scope' => implode(' ', $SCOPES))));
}

RSA-SHA1를 서명 방법으로 사용하는 경우:

require_once 'Zend/Crypt/Rsa/Key/Private.php';
require_once 'Zend/Oauth/Consumer.php';

session_start();

$CONSUMER_KEY = 'example.com';
$SCOPE = 'https://docs.google.com/feeds/';

$oauthOptions = array(
  'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
  'version' => '1.0',
  'consumerKey' => $CONSUMER_KEY,
  'consumerSecret' => new Zend_Crypt_Rsa_Key_Private(file_get_contents(realpath('/path/to/yourRSAPrivateKey.pem'))),
  'signatureMethod' => 'RSA-SHA1',
  'callbackUrl' => 'http://myapp.example.com/access_token.php',
  'requestTokenUrl' => 'https://www.google.com/accounts/OAuthGetRequestToken',
  'userAuthorizationUrl' => 'https://www.google.com/accounts/OAuthAuthorizeToken',
  'accessTokenUrl' => 'https://www.google.com/accounts/OAuthGetAccessToken'
);

$consumer = new Zend_Oauth_Consumer($oauthOptions);

if (!isset($_SESSION['ACCESS_TOKEN'])) {
  $_SESSION['REQUEST_TOKEN'] = serialize($consumer->getRequestToken(array('scope' => $SCOPE)));
}

Python

HMAC-SHA1를 서명 방법으로 사용하는 경우:

GDClient를 기반으로 하는 최신 v2.0 이상 클래스를 사용하는 경우 다음을 사용하세요.

import gdata.gauth
import gdata.docs.client

CONSUMER_KEY = 'example.com'
CONSUMER_SECRET = 'abc123doremi'
SCOPES = ['https://docs.google.com/feeds/', 'https://www.google.com/calendar/feeds/']  # example of a multi-scoped token

client = gdata.docs.client.DocsClient(source='yourCompany-YourAppName-v1')

oauth_callback_url = 'http://%s/get_access_token' % self.request.host
request_token = client.GetOAuthToken(
    SCOPES, oauth_callback_url, CONSUMER_KEY, consumer_secret=CONSUMER_SECRET)

# When using HMAC-SHA1, you need to persist the request_token in some way.
# You'll need the token secret when upgrading to an access token later on.
# In Google App Engine, you can use the AeSave helper:
# gdata.gauth.AeSave(request_token, 'myKey')

RSA-SHA1를 서명 방법으로 사용하는 경우:

...

f = open('/path/to/yourRSAPrivateKey.pem')
RSA_KEY = f.read()
f.close()

request_token = client.GetOAuthToken(SCOPES, oauth_callback_url, CONSUMER_KEY, rsa_private_key=RSA_KEY)

또는 GDataService를 기반으로 하는 이전 v1.0 클래스를 사용하는 경우 호출이 약간 다릅니다.

import gdata.auth
import gdata.docs.service

CONSUMER_KEY = 'example.com'
CONSUMER_SECRET = 'abc123doremi'

client = gdata.docs.service.DocsService(source='yourCompany-YourAppName-v1')
client.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.HMAC_SHA1, CONSUMER_KEY, consumer_secret=CONSUMER_SECRET)

req_token = client.FetchOAuthRequestToken()
client.SetOAuthToken(req_token)

RSA-SHA1를 서명 방법으로 사용하는 경우:

...

f = open('/path/to/yourRSAPrivateKey.pem')
RSA_KEY = f.read()
f.close()

client = gdata.docs.service.DocsService(source='yourCompany-YourAppName-v1')
client.SetOAuthInputParameters(gdata.auth.OAuthSignatureMethod.RSA_SHA1, CONSUMER_KEY, rsa_key=RSA_KEY)

SCOPES = ['https://docs.google.com/feeds/', 'https://www.google.com/calendar/feeds/']  # example of a multi-scoped token
req_token = client.FetchOAuthRequestToken(scopes=SCOPES)
client.SetOAuthToken(req_token)

.NET

HMAC-SHA1를 서명 방법으로 사용하는 경우:

using Google.GData.Client;

string CONSUMER_KEY = "example.com";
string CONSUMER_SECRET = "abc123doremi";

// Multi-scoped token.
string SCOPE = "https://www.google.com/calendar/feeds/ https://www.google.com/m8/feeds/";

OAuthParameters parameters = new OAuthParameters() {
  ConsumerKey = CONSUMER_KEY,
  ConsumerSecret = CONSUMER_SECRET,
  Scope = SCOPE,
  Callback = "http://myapp.example.com/access_token",
  SignatureMethod = "HMAC-SHA1"
}

OAuthUtil.GetUnauthorizedRequestToken(parameters);

RSA-SHA1를 서명 방법으로 사용하는 경우:

RSA-SHA1 is not supported yet.

요청 토큰 승인

요청 토큰을 승인하려면 애플리케이션이 사용자를 OAuthAuthorizeToken URL로 리디렉션해야 하며, 그러면 사용자에게 Google 계정에 로그인하라는 메시지가 표시됩니다. OAuthAuthorizeToken URL에 관한 자세한 내용은 전체 웹 애플리케이션용 OAuth 인증을 참고하세요.

애플리케이션에서 OAuthAuthorizeToken URL을 구성하려면 각 클라이언트 라이브러리에 다음을 사용하세요. 이 샘플은 이전 예시를 기반으로 빌드됩니다.

승인 페이지 URL을 구성한 후 애플리케이션은 다양한 방식으로 이를 사용하여 사용자를 OAuthAuthorizeToken 핸들러로 보낼 수 있습니다. 가장 일반적인 방법은 사용자를 리디렉션하거나 해당 페이지로 연결되는 링크를 표시하는 것입니다.

자바

HMAC-SHA1:

String approvalPageUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);
System.out.println(approvalPageUrl);

RSA-SHA1:

String approvalPageUrl = oauthHelper.createUserAuthorizationUrl(oauthParameters);
System.out.println(approvalPageUrl);

PHP

// If on a G Suite domain, use your domain for the hd param (e.g. 'example.com').
$approvalUrl = $consumer->getRedirectUrl(array('hd' => 'default'));
echo "<a href=\"$approvalUrl\">Grant access</a>";

또는 승인 URL로 간단히 리디렉션할 수 있습니다.

// If on a G Suite domain, use your domain for the hd param (e.g. 'example.com').
$consumer->redirect(array('hd' => 'default'));

Python

GDClient를 기반으로 하는 최신 v2.0 이상 클래스를 사용하는 경우 다음을 사용하세요.

# req_token is from previous call to client.GetOAuthToken()
domain = None  # If on a G Suite domain, use your domain (e.g. 'example.com').
self.redirect(request_token.generate_authorization_url(google_apps_domain=domain))

GDataService를 기반으로 하는 이전 v1.0 클래스를 사용하는 경우 프로세스가 약간 다릅니다.

# req_token is from previous call to client.FetchOAuthRequestToken()
oauth_callback_url = 'http://%s/get_access_token' % self.request.host
self.redirect(client.GenerateOAuthAuthorizationURL(callback_url=oauth_callback_url))

.NET

string authorizationUrl = OAuthUtil.CreateUserAuthorizationUrl(parameters);
Console.WriteLine(authorizationUrl);

콜백 URL에서 토큰 추출

Google이 애플리케이션으로 다시 리디렉션하면 oauth_token가 'oauth_callback_url' URL에 쿼리 매개변수로 추가됩니다. 그런 다음 애플리케이션이 URL 쿼리 매개변수에서 토큰 값을 추출하고 oauth 매개변수를 다시 설정해야 합니다.

클라이언트 라이브러리는 oauth_token를 추출하는 편리한 메서드를 제공합니다. 이 샘플은 이전 예시를 바탕으로 빌드됩니다.

자바

HMAC-SHA1 사용 시 콜백 URL에 토큰 비밀번호를 유지하도록 선택한 경우 다음 단계를 따르세요.

GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);

GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthHmacSha1Signer());
oauthHelper.getOAuthParametersFromCallback(request.getQueryString(), oauthParameters);

RSA-SHA1와의 유일한 차이점은 서명 방법입니다.

GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);

PrivateKey privKey = getPrivateKey("/path/to/your/rsakey.pk8");

GoogleOAuthHelper oauthHelper = new GoogleOAuthHelper(new OAuthRsaSha1Signer(privKey));
oauthHelper.getOAuthParametersFromCallback(request.getQueryString(), oauthParameters);

PHP

PHP 라이브러리를 사용하는 경우에는 이 단계가 필요하지 않습니다.

Python

GDClient를 기반으로 하는 최신 v2.0 이상 클래스를 사용하는 경우 다음을 사용하세요.

# Recall request_token. In Google App Engine, use AeLoad():
# saved_request_token = gdata.gauth.AeLoad('myKey')

request_token = gdata.gauth.AuthorizeRequestToken(saved_request_token, self.request.uri)

GDataService 기반의 이전 v1.0 클래스를 사용하는 경우 다음을 사용하세요.

oauth_token = gdata.auth.OAuthTokenFromUrl(self.request.uri)
if oauth_token:
  oauth_token.secret = # TODO: recall saved request_token and set the token secret here.
  oauth_token.oauth_input_params = gdata.auth.OAuthInputParams(
      gdata.auth.OAuthSignatureMethod.HMAC_SHA1, CONSUMER_KEY, consumer_secret=CONSUMER_SECRET)
 client.SetOAuthToken(oauth_token)
else:
  print 'No oauth_token found in the URL'

RSA-SHA1의 경우 프로세스가 유사하지만 토큰 보안 비밀이 없습니다.

oauth_token = gdata.auth.OAuthTokenFromUrl(self.request.uri)
if oauth_token:
  oauth_token.oauth_input_params = gdata.auth.OAuthInputParams(
      gdata.auth.OAuthSignatureMethod.RSA_SHA1, CONSUMER_KEY, rsa_key=RSA_KEY)
 client.SetOAuthToken(oauth_token)
else:
  print 'No oauth_token found in the URL'

.NET

콜백 URL에 토큰 보안 비밀을 유지하도록 선택한 경우:

OAuthUtil.UpdateOAuthParametersFromCallback(url, parameters);

액세스 토큰으로 업그레이드

OAuth 토큰 댄스의 마지막 단계는 전체 웹 애플리케이션용 OAuth 인증 문서에 설명된 대로 OAuthGetAccessToken URL을 사용하여 승인된 요청 토큰을 수명이 긴 액세스 토큰으로 업그레이드하는 것입니다.

다음은 각 클라이언트 라이브러리를 사용하는 몇 가지 예입니다.

자바

String accessToken = oauthHelper.getAccessToken(oauthParameters);
// You can also pull the OAuth token string from the oauthParameters:
// String accessToken = oauthParameters.getOAuthToken();
System.out.println("OAuth Access Token: " + accessToken);

String accessTokenSecret = oauthParameters.getOAuthTokenSecret();
System.out.println("OAuth Access Token's Secret: " + accessTokenSecret);

PHP

if (!isset($_SESSION['ACCESS_TOKEN'])) {
  if (!empty($_GET) && isset($_SESSION['REQUEST_TOKEN'])) {
    $_SESSION['ACCESS_TOKEN'] = serialize($consumer->getAccessToken($_GET, unserialize($_SESSION['REQUEST_TOKEN'])));
  }
}

Python

GDClient를 기반으로 하는 최신 v2.0 이상 클래스를 사용하는 경우 다음을 사용하세요.

# Upgrade the token and save in the user's datastore
access_token = client.GetAccessToken(request_token)

# If you're using Google App Engine, you can call the AeSave() method to save
# the access token under the current logged in user's account.
#gdata.gauth.AeSave(access_token, token_key)

GDataService 기반의 이전 v1.0 클래스를 사용하는 경우 다음을 사용하세요.

access_token = client.UpgradeToOAuthAccessToken()  # calls SetOAuthToken() for you

App Engine에서 gdata.gauth.AeSave()을 사용하는 경우 토큰과 토큰 보안 비밀이 현재 로그인한 사용자 아래에 저장됩니다.

.NET

OAuthUtil.GetAccessToken(parameters);

// If you want to extract the OAuth Token/TokenSecret from the OAuthParameters instance:
string accessToken = parameter.Token;
Console.WriteLine("OAuth Access Token: " + accessToken);

string accessTokenSecret = parameter.TokenSecret;
Console.WriteLine("OAuth Access Token's Secret: " + accessTokenSecret);

참고: HMAC-SHA1를 사용하는 경우 액세스 토큰의 토큰 보안 비밀을 데이터베이스의 토큰 값과 함께 저장해야 합니다. 그렇지 않으면 나중에 사용할 oauth 매개변수를 올바르게 재구성할 수 없습니다.

액세스 토큰 사용

액세스 토큰을 획득한 후 표준 Google Data API 클라이언트 라이브러리 호출을 사용하여 서비스와 상호작용합니다. 라이브러리에서 요청에 서명하고 올바른 승인 헤더를 포함합니다. 일반적으로 쿠키 또는 데이터베이스에서 사용자의 토큰을 호출합니다. 이 예시에서는 oauth 매개변수를 재구성하고 클라이언트 라이브러리 호출을 만드는 방법을 보여줍니다.

자바

HMAC-SHA1를 사용하는 경우

  GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
  oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
  oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
  oauthParameters.setOAuthToken(ACCESS_TOKEN);
  oauthParameters.setOAuthTokenSecret(TOKEN_SECRET);

  DocsService client = new DocsService("yourCompany-YourAppName-v1");
  client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());

  URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full");
  DocumentListFeed resultFeed = client.getFeed(feedUrl, DocumentListFeed.class);
  for (DocumentListEntry entry : resultFeed.getEntries()) {
    System.out.println(entry.getTitle().getPlainText());
  }
  

RSA-SHA1과의 차이점은 액세스 토큰의 보안 비밀을 설정할 필요가 없으며 서명자 객체를 구성하는 것이 다르다는 점입니다.

  GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
  oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
  oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
  oauthParameters.setOAuthToken(ACCESS_TOKEN);

  PrivateKey privKey = getPrivateKey("/path/to/your/rsakey.pk8");  // See above for the defintion of getPrivateKey()

  DocsService client = new DocsService("yourCompany-YourAppName-v1");
  client.setOAuthCredentials(oauthParameters, new OAuthRsaSha1Signer(privKey));

  URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full");
  DocumentListFeed resultFeed = client.getFeed(feedUrl, DocumentListFeed.class);
  for (DocumentListEntry entry : resultFeed.getEntries()) {
    System.out.println(entry.getTitle().getPlainText());
  }
  

PHP

require_once 'Zend/Gdata/Docs.php';

if (isset($_SESSION['ACCESS_TOKEN'])) {
  $accessToken = unserialize($_SESSION['ACCESS_TOKEN']);
} else {
  exit;
}


/*  Or, you could set an existing token (say one stored from your database). For HMAC-SHA1:
$accessToken = new Zend_Oauth_Token_Access();
$accessToken->setToken('1/AQfoI-qJDqkvvkf216Gc2g');
$accessToken->setTokenSecret('2c26GLW250tZiQ');
*/

$httpClient = $accessToken->getHttpClient($oauthOptions);
$client = new Zend_Gdata_Docs($httpClient, "yourCompany-YourAppName-v1");

// Retrieve user's list of Google Docs
$feed = $client->getDocumentListFeed();
foreach ($feed->entries as $entry) {
  echo "$entry->title\n";
}

Python

이 스니펫에서는 이미 액세스 토큰을 가져왔고 (HMAC-SHA1 사용) 나중에 사용할 토큰 키/비밀번호를 호출한다고 가정합니다.

GDClient를 기반으로 하는 최신 v2.0 이상 클래스를 사용하는 경우 다음을 사용하세요.

client = gdata.docs.client.DocsClient(source='yourCo-yourAppName-v1')
client.auth_token = gdata.gauth.OAuthHmacToken(CONSUMER_KEY, CONSUMER_SECRET, TOKEN,
                                               TOKEN_SECRET, gdata.gauth.ACCESS_TOKEN)
feed = client.GetDocList()
for entry in feed.entry:
  print entry.title.text

GDataService 기반의 이전 v1.0 클래스를 사용하는 경우 다음을 사용하세요.

client = gdata.docs.service.DocsService(source='yourCompany-YourAppName-v1')
client.SetOAuthInputParameters(SIG_METHOD, CONSUMER_KEY, consumer_secret=CONSUMER_SECRET)

# the token key and secret should be recalled from your database
client.SetOAuthToken(gdata.auth.OAuthToken(key=TOKEN, secret=TOKEN_SECRET))

feed = client.GetDocumentListFeed()
for entry in feed.entry:
  print entry.title.text

.NET

HMAC-SHA1를 사용하는 경우

OAuthParameters parameters = new OAuthParameters() {
  ConsumerKey = CONSUMER_KEY,
  ConsumerSecret = CONSUMER_SECRET,
  Token = ACCESS_TOKEN,
  TokenSecret = TOKEN_SECRET
}

GOAuthRequestFactory requestFactory = new GOAuthRequestFactory("writely", APPLICATION_NAME, parameters);

DocsService service = new DocsService(APPLICATION_NAME);
service.RequestFactory = requestFactory;

DocumentsListQuery query = new DocumentsListQuery();
DocumentsFeed feed = service.Query(query);
foreach (DocumentEntry entry in feed.Entries) {
  Console.WriteLine(entry.Title.Text);
}

RSA-SHA1과의 차이점은 액세스 토큰의 보안 비밀을 설정할 필요가 없으며 서명자 객체를 구성하는 것이 다르다는 점입니다.

RSA-SHA1 is not supported yet.

추가 3-Legged OAuth 리소스 및 샘플

맨 위로

2 Legged OAuth

2레그 OAuth를 사용하면 신뢰할 수 있는 애플리케이션이 사용자의 직접적인 참여 없이 사용자의 Google 데이터에 액세스할 수 있습니다. 다음 두 가지 주요 그룹은 2-legged OAuth를 사용할 수 있습니다.

G Suite 도메인 관리자: 관리자는 Google Data API를 통해 도메인의 사용자 데이터를 관리하는 스크립트와 맞춤 애플리케이션을 빌드할 수 있습니다. G Suite 도메인과 연결된 키 및 보안 비밀을 관리하고 전역 액세스 제어를 부여하는 방법을 알아보려면 'OAuth 키 및 보안 비밀 관리'를 참고하세요.

서드 파티 소프트웨어 공급업체: 공급업체는 2단계 OAuth를 사용하여 G Suite와 통합되는 애플리케이션을 제공할 수 있습니다. 서드 파티 애플리케이션에 대한 액세스 권한은 API 클라이언트 관리 페이지에서 부여하거나 G Suite Marketplace에서 설치하여 부여할 수 있습니다.

액세스 토큰은 일반 승인 흐름 (3-legged OAuth라고도 함)에 따라 필요하지 않습니다.

다음 클라이언트 라이브러리 샘플은 HMAC-SHA1를 사용하여 2단계 OAuth를 사용하도록 클라이언트를 설정하는 방법을 보여줍니다.

자바

import com.google.gdata.client.docs.*;
import com.google.gdata.client.authn.oauth.*;

String CONSUMER_KEY = "example.com";
String CONSUMER_SECRET = "abc123doremi";

GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);

DocsService client = new DocsService("yourCompany-YourAppName-v1");
client.setOAuthCredentials(oauthParameters, new OAuthHmacSha1Signer());

// Retrieve user's list of Google Docs
String user = "any.user@anydomain.com";
URL feedUrl = new URL("https://docs.google.com/feeds/default/private/full" +
                      "?xoauth_requestor_id=" + user);

DocumentListFeed resultFeed = client.getFeed(feedUrl, DocumentListFeed.class);
for (DocumentListEntry entry : resultFeed.getEntries()) {
  System.out.println(entry.getTitle().getPlainText());
}

PHP

require_once 'Zend/Oauth/Consumer.php';
require_once 'Zend/Gdata/Docs.php';

$CONSUMER_KEY = 'example.com';
$CONSUMER_SECRET = 'abc123doremi';
$USER = 'any.user@anydomain.com';

$oauthOptions = array(
    'requestScheme' => Zend_Oauth::REQUEST_SCHEME_HEADER,
    'version' => '1.0',
    'signatureMethod' => 'HMAC-SHA1',
    'consumerKey' => $CONSUMER_KEY,
    'consumerSecret' => $CONSUMER_SECRET
);

$consumer = new Zend_Oauth_Consumer($oauthOptions);
$token = new Zend_Oauth_Token_Access();
$httpClient = $token->getHttpClient($oauthOptions);

$client = new Zend_Gdata_Docs($httpClient);

// Retrieve user's list of Google Docs
$feed = $client->getDocumentListFeed('https://docs.google.com/feeds/default/private/full?xoauth_requestor_id=' . urlencode($USER));
foreach ($feed->entries as $entry) {
  echo "$entry->title\n";
}

Python

GDClient를 기반으로 하는 최신 v2.0 이상 클래스를 사용하는 경우 다음을 사용하세요.

import gdata.gauth
import gdata.docs.client

CONSUMER_KEY = 'example.com'
CONSUMER_SECRET = 'abc123doremi'
requestor_id = 'any.user@anydomain.com'

client = gdata.docs.client.DocsClient(source='yourCompany-YourAppName-v1')
client.auth_token = gdata.gauth.TwoLeggedOAuthHmacToken(
    CONSUMER_KEY, CONSUMER_SECRET, requestor_id)

# Retrieve user's list of Google Docs
feed = client.GetDocList()
for entry in feed.entry:
  print entry.title.text

GDataService 기반의 이전 v1.0 클래스를 사용하는 경우 다음을 사용하세요.

import gdata.auth
import gdata.docs.service

CONSUMER_KEY = 'example.com'
CONSUMER_SECRET = 'abc123doremi'
SIG_METHOD = gdata.auth.OAuthSignatureMethod.HMAC_SHA1

requestor_id = 'any.user@anydomain.com'

client = gdata.docs.service.DocsService(source='yourCompany-YourAppName-v1')
client.SetOAuthInputParameters(SIG_METHOD, CONSUMER_KEY, consumer_secret=CONSUMER_SECRET,
                               two_legged_oauth=True, requestor_id=requestor_id)

# Retrieve user's list of Google Docs
feed = client.GetDocumentListFeed()
for entry in feed.entry:
  print entry.title.text

# Change to another user on your domain
client.GetOAuthInputParameters().requestor_id = 'another.user@example.com'

.NET

using Google.GData.Client;
using Google.GData.Documents;

// Create an OAuth factory to use
GOAuthRequestFactory requestFactory = new GOAuthRequestFactory("writely", "yourCompany-YourAppName-v1");
requestFactory.ConsumerKey = "example.com";
requestFactory.ConsumerSecret = "abc123doremi";

String user = "any.user@anydomain.com";

DocumentsService client = new DocumentsService("yourCompany-YourAppName-v1");
client.RequestFactory = requestFactory;

// Retrieve user's list of Google Docs
DocumentsListQuery query = new DocumentsListQuery();
query.Uri = new OAuthUri("https://docs.google.com/feeds/default/private/full", user, requestFactory.ConsumerKey);

DocumentsFeed feed = client.Query(query);

foreach (DocumentEntry entry in feed.Entries)
{
  Console.WriteLine(entry.Title.Text);
}

추가 2-Legged OAuth 리소스 및 샘플

자체 서명 비공개 키 및 공개 인증서 생성

비공개 키는 서명을 생성하는 데 사용되며, 이 서명은 각 요청에 포함되어야 합니다. 인증서에 삽입된 공개 키는 Google에서 서명을 확인하는 데 사용됩니다. 공개 키는 PEM 형식의 X.509 인증서로 인코딩된 1024비트 RSA 키여야 합니다. 인증서는 등록 시점에 Google에 전송해야 합니다.

다음 섹션에서는 OpenSSL 유틸리티와 Java의 keytool 유틸리티라는 두 가지 특정 도구를 사용하여 키와 인증서를 생성하는 방법을 보여주는 예를 제공합니다.

이 예는 Google Data API에만 해당하지 않습니다. 동일한 유틸리티를 사용하여 어떤 용도로든 키를 생성할 수 있습니다.

이 예에서는 회사 이름이 My_Company이고, 미국 캘리포니아주 마운틴뷰에 있으며, 도메인 이름이 example.com이라고 가정합니다.

OpenSSL을 사용하여 키 생성

RSA 키 쌍과 해당 인증서를 만들려면 다음 명령어를 사용하면 됩니다.

# Generate the RSA keys and certificate
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -sha1 -subj \
  '/C=US/ST=CA/L=Mountain View/CN=www.example.com' -keyout \
  myrsakey.pem -out /tmp/myrsacert.pem

경고: -nodes 매개변수를 포함하면 보호할 비밀번호가 없는 비공개 키가 생성됩니다. 하지만 보안 강화를 위해 이 매개변수를 생략하는 것이 좋습니다.

-sha1 매개변수는 키가 SHA1 서명을 생성하는 데 사용됨을 지정합니다.

-subj 매개변수는 인증서가 나타내는 애플리케이션의 ID를 지정합니다.

-keyout 매개변수는 키가 포함될 파일을 지정합니다. 이 파일에는 민감한 정보가 포함되어 있으므로 보호해야 하며 누구와도 공유해서는 안 됩니다.

-out 매개변수는 PEM 형식의 인증서가 포함된 파일(등록 시 Google에 전송 가능)을 지정합니다.

.NET 클라이언트용 키 생성

.NET 프레임워크는 PEM 형식으로 저장된 키나 인증서를 인식하지 못합니다. 따라서 .pem 파일을 만든 후 추가 단계가 필요합니다.

openssl pkcs12 -export -in test_cert.pem -inkey myrsacert.pem -out myrsacert.pfx -name "Testing Certificate"

이 단계에서는 비공개 키와 인증서에서 PFX 파일을 생성합니다. 이 파일은 .NET 클라이언트 라이브러리로 가져와 Google Data API에 대한 요청을 디지털 서명할 수 있습니다.

Java 클라이언트용 키 생성

Java 클라이언트는 PKCS#8 형식의 비공개 키를 허용합니다. 위의 안내를 사용하여 키/인증서를 생성한 후 생성된 .pem 파일에서 .pk8 파일을 만듭니다.

openssl pkcs8 -in myrsakey.pem -topk8 -nocrypt -out myrsakey.pk8

또는 Java 키 저장소와 keytool 유틸리티를 사용하여 RSA 키 쌍과 해당 인증서를 만들 수 있습니다. 다음 명령어를 사용하세요.

# Generate the RSA keys and certificate
keytool -genkey -v -alias Example -keystore ./Example.jks\
  -keyalg RSA -sigalg SHA1withRSA\
  -dname "CN=www.example.com, OU=Engineering, O=My_Company, L=Mountain  View, ST=CA, C=US"\
  -storepass changeme -keypass changeme

경고: 'changeme'은 안전한 비밀번호가 아닙니다. 이는 예시일 뿐입니다.

-dname 매개변수는 인증서가 나타내는 애플리케이션의 ID를 지정합니다. -storepass 매개변수는 키 저장소를 보호하는 비밀번호를 지정합니다. -keypass 매개변수는 비공개 키를 보호하는 비밀번호를 지정합니다.

ManageDomains 도구에서 사용할 수 있는 파일에 인증서를 쓰려면 다음 명령어를 사용합니다.

# Output the public certificate to a file
keytool -export -rfc -keystore ./Example.jks -storepass changeme \
  -alias Example -file mycert.pem

맨 위로