0

, ClientIdserviceaccount 생성되었다 client_secret.json 파일에 clientsecret 값없이 serviceaccount 권한을 부여하는 방법. client_secret.json 파일을 다운로드 할 때 ClientId 값만 있고 clientsecret 값이 없습니다. clientsecret 값이 없으면 고객을 어떻게 승인합니까?의 OAuth 2.0 클라이언트 ID와 내가 <code>serviceaccount</code> 자격 증명을 생성 할 때

우리는 console.developers.com에 드롭 다운하고, googleauthorizationflow를 사용하여 우리의 클라이언트에 권한을 부여 serviceaccount에 대한 GoogleCredential 객체에 생성 된 액세스 토큰을 전달하고 요청을 만들기 자격 증명을 다시 OAuth 2.0 ClientId을 생성해야 하는가?

자격 등급 :

public static Credential authorize() throws IOException { 
     // Load client secrets. 
     InputStream in = Quickstart.class 
      .getResourceAsStream("/client_secret.json"); 
     GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
      JSON_FACTORY, new InputStreamReader(in)); 

     // Build flow and trigger user authorization request. 
     GoogleAuthorizationCodeFlow flow =new GoogleAuthorizationCodeFlow.Builder(
       HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 
      .setDataStoreFactory(DATA_STORE_FACTORY) 
      .setAccessType("offline").setApprovalPrompt("auto").build(); 
     Credential credential = new AuthorizationCodeInstalledApp(flow, 
      new LocalServerReceiver.Builder() 
         .setHost("localhost").build()).authorize("user"); 

     GoogleCredential gCredential = new GoogleCredential.Builder() 
      .setTransport(HTTP_TRANSPORT) 
      .setJsonFactory(JSON_FACTORY) 
      .setServiceAccountId(serviceaccountemail) 
      .setServiceAccountPrivateKeyFromP12File(new File("privatekeyfile.p12")) 
      .setServiceAccountScopes(SCOPES) 
      .setServiceAccountUser(useremail) 
      .setClientSecrets(clientSecrets) 
      .build().setAccessToken(credential.getAccessToken()) 
      .setRefreshToken(credential.getRefreshToken()) 
      .setExpirationTimeMilliseconds(
        credential.getExpirationTimeMilliseconds()); 

     System.out.println("Credentials saved to " + 
      DATA_STORE_DIR.getAbsolutePath()); 
     return credential; 
    } 

답변

0

서비스 계정 및 OAuth2를 사이에 차이가 있습니다. 사용자의 개인 데이터에 액세스 할 수 있도록 사용자를 인증하려고하는 경우 Oauth2와 함께 가야합니다. 개인적으로 소유하고 사용자와 공유하는 데이터에 액세스하려는 경우 서비스 계정을 사용해야합니다.

json 키 파일은 사용하는 데 필요한 코드와 다릅니다. 실수로 서비스 계정을 만든 경우 삭제하고 Oauth2 자격 증명을 만드는 것이 좋습니다.

+0

답장을 보내 주셔서 감사합니다. 도메인을 보유하고 있는데 일부 사용자를 생성하고 일부 약속을 만들었습니다. 이제 서비스 계정을 사용하여 캘린더 정보에 액세스하려면 기존 코드 외에 Quickstart.java의 serviceaccount에 대한 GoogleCredentials를 작성해야합니다. – test

+0

당신은 도메인의 일부 계정에 계정에 서비스 계정 액세스 권한을 부여해야 할 것입니다. – DaImTo

+0

일 것입니다. json 서비스 계정 파일이 아닌 p12 파일을 다운로드 한 것일 수 있습니다. – DaImTo