向请求授权

您的应用向 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 为已安装的应用配置客户端以及向请求授权。我们的示例和库页面上提供了其他语言。

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