요청 승인

애플리케이션에서 Manufacturer Center API로 전송하는 모든 요청에는 승인 토큰이 포함되어야 합니다. 이 토큰은 Google에 애플리케이션 식별 정보를 제공하기도 합니다.

승인 프로토콜 정보

요청을 승인하려면 애플리케이션에서 OAuth 2.0을 사용해야 합니다. 다른 승인 프로토콜은 지원되지 않습니다. 애플리케이션에서 Google 계정으로 로그인을 사용하는 경우 승인의 일부 절차는 자동으로 처리됩니다.

OAuth 2.0을 사용하여 요청 승인

Manufacturer Center API에 대한 모든 요청은 인증된 사용자의 승인을 받아야 합니다.

OAuth 2.0의 승인 과정 세부사항('흐름')은 만들고 있는 애플리케이션의 종류에 따라 다소 다릅니다. 다음의 일반적인 과정은 모든 애플리케이션 유형에 적용됩니다.

  1. 애플리케이션을 만들 때 Google API 콘솔을 사용하여 애플리케이션을 등록합니다. 이렇게 하면 Google에서 클라이언트 ID 및 클라이언트 보안 비밀번호와 같이 나중에 필요한 정보를 제공합니다.
  2. Google API 콘솔에서 Manufacturer Center API를 활성화합니다. API 콘솔의 목록에 이 API가 없다면 이 단계를 건너뜁니다.
  3. 애플리케이션에서 사용자 데이터에 액세스해야 하는 경우 Google에 특정 액세스 범위를 요청합니다.
  4. Google에서 사용자에게 애플리케이션의 데이터 요청을 승인할 것인지 물어보는 동의 화면을 표시합니다.
  5. 사용자가 승인하면 Google에서 애플리케이션에 제한 시간이 있는 단기 액세스 토큰을 제공합니다.
  6. 애플리케이션에서 액세스 토큰을 첨부하여 사용자 데이터를 요청합니다.
  7. Google에서 사용자의 요청과 토큰이 유효하다고 판단하면 요청된 데이터를 반환합니다.

일부 흐름에는 새로운 액세스 토큰을 얻기 위해 갱신 토큰을 사용하는 등의 추가 단계가 포함됩니다. 다양한 유형의 애플리케이션 흐름에 관한 자세한 내용은 Google의 OAuth 2.0 문서를 참조하세요.

Manufacturer Center API의 OAuth 2.0 범위 정보는 다음과 같습니다.

범위 의미
https://www.googleapis.com/auth/manufacturercenter 읽기/쓰기 액세스

OAuth 2.0을 사용하여 액세스를 요청하려면 애플리케이션에 범위 정보와 함께 애플리케이션을 등록할 때 Google에서 제공하는 정보(예: 클라이언트 ID, 클라이언트 보안 비밀)가 필요합니다.

팁: Google API 클라이언트 라이브러리가 사용자를 대신하여 일부 승인 과정을 처리할 수 있습니다. 여러 가지 프로그래밍 언어로 제공되므로 자세한 내용은 라이브러리 및 샘플 페이지를 참조하세요.

승인 예시

다음 코드는 설치된 애플리케이션에 대해 OAuth 2.0을 사용하여 클라이언트를 구성하고 요청을 승인하는 방법을 보여줍니다. 다른 언어는 샘플 및 라이브러리 페이지에서 확인할 수 있습니다.

자바

설치된 애플리케이션에 OAuth 2.0 사용에 설명된 명령줄 승인 코드 흐름입니다.

Content API 자바 샘플 코드의 스니펫 예:

    public static void main(String[] args) {
      try {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = new FileDataStoreFactory(DATA_STORE_DIR);
        jsonFactory = JacksonFactory.getDefaultInstance();
        scopes =  "https://www.googleapis.com/auth/manufacturercenter";

        // load configuration
        File configPath = new File(basePath, "manufacturers");
        File configFile = new File(configPath, manufacturers-info.json);
        ManufacturersConfig config = new JacksonFactory().fromInputStream(inputStream, ManufacturersConfig.class);
        config.setPath(configPath);

        // Get authorization token
        Credential credential = authenticate(httpTransport, dataStoreFactory, config, jsonFactory, scopes);
        // ...
      }
    }

    private static Credential authenticate(httpTransport, dataStoreFactory, config, jsonFactory, scopes) throws Exception {
      try {
        Credential credential = GoogleCredential.getApplicationDefault().createScoped(scopes);
        System.out.println("Loaded the Application Default Credentials.");
        return credential;
      } catch (IOException e) {
        // No need to do anything, we'll fall back on other credentials.
      }
      if (config.getPath() == null) {
        throw new IllegalArgumentException(
            "Must use Application Default Credentials with no configuration directory.");
      }
      File clientSecretsFile = new File(config.getPath(), "client-secrets.json");
      if (clientSecretsFile.exists()) {
        System.out.println("Loading OAuth2 client credentials.");
        try (InputStream inputStream = new FileInputStream(clientSecretsFile)) {
          GoogleClientSecrets clientSecrets =
              GoogleClientSecrets.load(jsonFactory, new InputStreamReader(inputStream));
          // set up authorization code flow
          GoogleAuthorizationCodeFlow flow =
              new GoogleAuthorizationCodeFlow.Builder(
                      httpTransport, jsonFactory, clientSecrets, scopes)
                  .setDataStoreFactory(dataStoreFactory)
                  .build();
          // authorize
          String userID = ConfigDataStoreFactory.UNUSED_ID;
          Credential storedCredential = flow.loadCredential(userID);
          if (storedCredential != null) {
            System.out.printf("Retrieved stored credential for %s from cache.%n", userID);
            return storedCredential;
          }
          LocalServerReceiver receiver =
              new LocalServerReceiver.Builder().setHost("localhost").setPort(9999).build();
          Credential credential = new AuthorizationCodeInstalledApp(flow, receiver).authorize(userID);
          System.out.printf("Retrieved credential for %s from web.%n", userID);
          return credential;
        } catch (IOException e) {
          throw new IOException(
              "Could not retrieve OAuth2 client credentials from the file "

                                    +   clientSecretsFile.getCanonicalPath());
        }
      }
      throw new IOException(
          "No authentication credentials found. Checked the Google Application"
                            +   "Default Credentials and the paths "
                            +   clientSecretsFile.getCanonicalPath()
                            +   ". Please read the accompanying README.");
    }