リクエストの承認

アプリケーションが 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 を有効にします。(Indexing API が API Console に表示されない場合は、この手順をスキップしてください)。
  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 を使用してリクエストを承認する方法を示しています。その他の言語については、サンプルとライブラリのページをご覧ください。

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