3

드라이브 서비스 계정을 인스턴스화하기위한 샘플 코드를 얻는 데 문제가 있습니다. API 콘솔에 서비스 계정을 설정하고 'https://www.googleapis.com/auth/drive'의 범위를 포함 시켰지만이를 실행하면 "인증에 실패했습니다. 서버 메시지 : (Signet :: AuthorizationError)"오류가 발생합니다.Ruby의 Google Apps API 및 서비스 계정 관련 문제

이상하게도 user_email 주소를 생략해도 오류는 발생하지 않습니다.

내 목표는 조직의 드라이브에 저장된 모든 파일에 대한 감사를 수행하는 것이고, 서비스 계정을 사용하면 저장된 모든 파일의 목록을 가져 오는 방법이라고 생각합니다.

서버 측에서 특별한 설정이 필요하지 않으셨습니까? 당신이 이전 예제를 사용하는 것처럼

require 'google/api_client' 

## Email of the Service Account # 
SERVICE_ACCOUNT_EMAIL = '<service account email>@developer.gserviceaccount.com' 

## Path to the Service Account's Private Key file # 
SERVICE_ACCOUNT_PKCS12_FILE_PATH = '<private key file>-privatekey.p12' 

def build_client(user_email) 
    key = Google::APIClient::PKCS12.load_key(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'notasecret') 
    asserter = Google::APIClient::JWTAsserter.new(SERVICE_ACCOUNT_EMAIL, 'https://www.googleapis.com/auth/drive', key) 
    client = Google::APIClient.new 

    client.authorization = asserter.authorize(user_email) 
    return client 
end 

client = build_client("<users email address>") 

답변

8

이 나에게 보인다. 제 생각에 1 년 전에 그렇게 해왔 던 것 같아요. 2012 년 말에 Signet이 OAuth2 설정의 모든 측면을 처리하도록 업데이트 되었기 때문에 앱 설정 방법이 사용되지 않았습니다.

다음은 일반적으로 서비스 계정을 만드는 데 사용하는 코드입니다. 방법에 맞게 조정할 수 있습니다.

client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token', 
:audience => 'https://accounts.google.com/o/oauth2/token', 
:scope => "https://www.googleapis.com/auth/drive", 
:issuer => "<service account email>@developer.gserviceaccount.com", 
:signing_key => Google::APIClient::KeyUtils.load_from_pkcs12("<private key file>-privatekey.p12", "notasecret"), 
:person => "<users email address>") 
client.authorization.fetch_access_token! 

아직 문제가있는 경우 알려 주시면 도와 드리겠습니다.

+0

흥미 롭군요. 마침내 제 코드로 작업하게되었습니다. 나는 어떤 단계에서 오타가 있었음에 틀림 없다. 그러나 나는 모든 것을 세 번 점검하겠다고 맹세한다. – JRQ

+0

이상합니다. [여기 Google 문서] (https://developers.google.com/drive/web/delegation)에 표시된 루비 예제는 OP와 비슷합니다. Signet 방법에 대한 문서가 있습니까? – mmcrae

+0

신난다,이 대답은 여기에서 유용한 요청을 요청했다! https://github.com/northworld/google_calendar/pull/95 –

2

Java를 사용하여 서비스 계정 + 드라이브 + 파일 권한으로 작업했습니다. 특정 사용자에 대한 사용 권한을 사용하려면 특정 범위를 허용해야했습니다. 귀하의 문제에 대해 추측 할 수있는 것은 내가 누락되었을 가능성이 있다는 것입니다. the Delegation part

+0

감사합니다. 나는 여러 번 돌아가서 모든 과정을 처음부터 다시했다. 세 번째로 작동하기 시작 했으므로 어떤 단계에서 잘못된 것을 붙여 넣었어야한다고 가정해야합니다. – JRQ

2

google-api-client의 버전 0.9.13을 사용하여 다음과 같은 약간의 우드 워드 응답 적응을 성공 시켰습니다. 매개 변수) :

def service_account_authorization(credentials_file, scope) 
    credentials = JSON.parse(File.open(credentials_file, 'rb').read) 
    authorization = Signet::OAuth2::Client.new(
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token', 
    :audience => 'https://accounts.google.com/o/oauth2/token', 
    :scope => scope, 
    :issuer => credentials['client_id'], 
    :signing_key => OpenSSL::PKey::RSA.new(credentials['private_key'], nil), 
) 
    authorization.fetch_access_token! 
    authorization 
end 

이 조각은이 서비스 계정에 Google 클라우드 콘솔에서 다운로드로 파일을 받아 구글 :: API를 :: * Service.authorization에 공급 할 수있는 인증 개체를 반환합니다.

감사합니다. James!

관련 문제