リクエストの承認

アプリケーションから Manufacturer Center API に送信するリクエストには、必ず承認トークンが含まれている必要があります。このトークンは Google でアプリケーションを識別するためにも使用されます。

認証プロトコルについて

リクエストを承認するために、アプリケーションは OAuth 2.0 を使用する必要があります。これ以外の認証プロトコルには対応していません。アプリケーションで「Google でログイン」を使用している場合、承認手続きの一部が自動化されます。

OAuth 2.0 を使用したリクエストの承認

Manufacturer Center API へのリクエストはすべて、認証されたユーザーによって承認される必要があります。

OAuth 2.0 の承認プロセス(「フロー」)の詳細は開発するアプリケーションの種類によって若干異なりますが、次の一般的なプロセスはすべての種類のアプリケーションに当てはまります。

  1. アプリケーションの作成時に、Google API Console を使用してアプリケーションを登録します。登録すると、後で必要になるクライアント ID や クライアント シークレットなどの情報が Google から提供されます。
  2. Google API Console で Manufacturer Center API を有効にします(Manufacturer Center API が API Console に表示されない場合は、この手順をスキップしてください)。
  3. アプリケーションでユーザーデータにアクセスする必要がある場合は、特定のアクセスのスコープを Google にリクエストします。
  4. データをリクエストするアプリケーションの承認を求める Google の同意画面がユーザーに表示されます。
  5. ユーザーが承認すると、有効期間の短いアクセス トークンがアプリケーションに付与されます。
  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 を使用して、クライアントを設定しリクエストを認可する方法を示しています。他の言語については、 サンプルとライブラリのページをご覧ください。

Java

これは、インストール型アプリケーションで OAuth 2.0 を使用する で説明されているコマンドライン認可コードフローです。

Content API の Java サンプル コードからのスニペットの例:

    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.");
    }