對要求進行授權

您的應用程式傳送至 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.");
   
}