אישור בקשות

כל הבקשות שהאפליקציה שולחת ל-Manufacturer Center API חייבות לכלול אסימון הרשאה. אסימון ההרשאה גם מזהה את האפליקציה שלכם ב-Google.

הסבר על פרוטוקולים של הרשאות

כדי לאשר בקשות, האפליקציה חייבת להשתמש בפרוטוקול OAuth 2.0. אין תמיכה בפרוטוקולים אחרים של הרשאות. אם האפליקציה משתמשת בכניסה באמצעות חשבון Google, היבטים מסוימים של ההרשאות מטופלים באופן אוטומטי.

הרשאת בקשות עם פרוטוקול OAuth 2.0

כל הבקשות ל-Manufacturer Center API חייבות לקבל הרשאה ממשתמש מאומת.

הפרטים או ה"זרימה" של תהליך ההרשאה עם OAuth 2.0 עשויים להשתנות מעט, בהתאם לסוג האפליקציה שאתם מפתחים. התהליך הכללי הבא חל על כל סוגי האפליקציות:

  1. כשאתם מפתחים את האפליקציה, צריך לרשום אותה באמצעות Google API Console. לאחר הרישום, Google מספקת נתונים שיהיו דרושים לכם מאוחר יותר, כמו מזהה לקוח וסוד לקוח.
  2. מפעילים את Manufacturer Center API ב-Google API Console. (אם ממשק ה-API לא מופיע במסוף ה-API, אפשר לדלג על השלב הזה.)
  3. כשהאפליקציה צריכה גישה לנתונים של משתמשים, היא מעבירה ל-Google בקשת גישה בהיקף ספציפי.
  4. Google מציגה למשתמש מסך הסכמה ומבקשת לאשר לאפליקציה לשלוח בקשה לחלק מהנתונים שלו.
  5. אם המשתמש מסכים, האפליקציה מקבלת מ-Google אסימון גישה לטווח קצר.
  6. האפליקציה מבקשת את נתוני המשתמש ומצרפת לבקשה את אסימון הגישה.
  7. אם Google תקבע שהבקשה והאסימון תקפים, היא תחזיר את הנתונים המבוקשים.

חלק מתהליכי העבודה כוללים שלבים נוספים, כמו שימוש באסימוני רענון כדי לקבל אסימוני גישה חדשים. למידע מפורט על תהליכי העבודה לסוגים שונים של אפליקציות, ניתן לעיין בתיעוד של OAuth 2.0 של Google.

הפרטים לגבי היקפי OAuth 2.0 ב-Manufacturer Center API:

היקף משמעות
https://www.googleapis.com/auth/manufacturercenter גישת קריאה/כתיבה.

כדי לבקש גישה באמצעות פרוטוקול OAuth 2.0, האפליקציה שלכם זקוקה למידע על ההיקף ולמידע ש-Google מספקת בזמן רישום האפליקציה (כמו מזהה לקוח וסוד לקוח).

טיפ: ספריות הלקוח של Google APIs יכולות לטפל עבורכם בחלק מתהליך ההרשאה. הן זמינות למגוון שפות תכנות. לפרטים נוספים, עיינו בדף עם ספריות ודוגמאות.

דוגמה להרשאה

הקוד הבא מדגים איך להגדיר את הלקוח ולתת הרשאה לבקשות באמצעות OAuth 2.0 לאפליקציות מותקנות. שפות נוספות זמינות בדף דוגמאות וספריות.

Java

זהו תהליך קוד ההרשאה של שורת הפקודה, המתואר בקטע שימוש ב-OAuth 2.0 לאפליקציות מותקנות.

קטע קוד לדוגמה מתוך קוד הדוגמה של Java ל-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.");
    }