Sử dụng tài khoản dịch vụ OAuth 2.0 với API Manufacturer Center

Tài khoản dịch vụ là các Tài khoản Google đặc biệt mà các ứng dụng có thể dùng để truy cập vào các API của Google theo phương thức lập trình thông qua OAuth 2.0. Tài khoản dịch vụ sử dụng quy trình OAuth 2.0 không yêu cầu uỷ quyền bổ sung. Thay vào đó, quy trình này sử dụng tệp khóa mà chỉ ứng dụng của bạn mới có thể truy cập. Hướng dẫn này thảo luận cách truy cập vào Manufacturer Center API bằng tài khoản dịch vụ.

Điều kiện tiên quyết

Các bước để sử dụng tài khoản dịch vụ để truy cập vào API Manufacturer Center

  1. Tạo thông tin xác thực của tài khoản dịch vụ hoặc truy cập vào thông tin xác thực công khai mà bạn đã tạo. Bạn sẽ cần tạo một mã ứng dụng khách OAuth 2.0 và lấy tệp khóa riêng tư *.json:

    1. Chuyển đến Google API Console.

    2. Chọn một dự án trong trình đơn thả xuống ở đầu trang. Nếu bạn chưa có, hãy tạo một tài khoản bằng cách nhấp vào Tạo dự án.

    3. Nếu bạn chưa bật API Manufacturer Center cho dự án này, hãy tìm kiếm trong danh sách API của Google và bật API đó.

    4. Trong thanh bên ở phía bên trái, hãy chọn Thông tin xác thực.

    5. Để thiết lập tài khoản dịch vụ, hãy chọn Create credential (Tạo thông tin xác thực), sau đó chọn Service account key (Khoá tài khoản dịch vụ). ![Tạo thông tin đăng nhập tài khoản dịch vụ

ảnh chụp màn hình.](../images/create_service_account_credentials.png)

6.  On the next page, select **New service account** from the drop-down
    list.

7.  <a name="service-account-name"></a> Name the new service account. This
    also serves as the default username for the service account ID. Remember
the service account ID for use later.

Note: The choice of role for the service account will not have any effect on
what calls can be made to the Manufacturer Center API, as access to
    Manufacturer Center API methods is determined instead by the role
    associated with the service account ID in Manufacturer Center. If you
    are unsure what to pick, just pick Project►Viewer.

![New service account name

ảnh chụp màn hình.](../images/new_service_account_name.png)

8.  Select **JSON** for the key type, then click **Create**.

9.  The **Create** button will change to **Creating...**, and once the key
    generation finishes, it will automatically download the private key as a
*.json file.

Important: Protect the *.json key file that allows a service account to
    access the Google services for which it has been authorized. It is good
practice to allow service accounts to only access one Google API each. This
is a preventative measure to mitigate the amount of data an attacker can
    access in the situation that the service account's *.json key file is
    compromised.

10.  You will be returned to the Credentials page, and you should see the
    new service account in the list of service account keys for your
    account.
  1. Thêm tài khoản dịch vụ mới làm người dùng vào tài khoản Manufacturer Center. Nếu là nhà phát triển bên thứ ba, bạn sẽ cần yêu cầu khách hàng làm bước này cho bạn.

    1. Đăng nhập vào tài khoản Manufacturer Center.

    2. Chuyển đến danh sách "Người dùng" trong phần cài đặt của tài khoản Manufacturer Center.

    3. Nhấp vào nút +Người dùng và sử dụng mã tài khoản dịch vụ làm địa chỉ email cho người dùng mới. ![Thêm nút người dùng tài khoản dịch vụ

ảnh chụp màn hình.](../images/add_service_account_network_settings.png)

4.  Fill in the form with the [service account ID](#service-account-name),
    and select the desired user role(s). At least one must be chosen, and
    use of the `Accounts` service requires the Admin role.

If you did not take note of the service account ID earlier, go to the
    [Service
    Accounts](https://console.developers.google.com/iam-admin/serviceaccounts/)
administration page and select the project you created.

![Add service account user page

ảnh chụp màn hình.](../images/add_service_account_page.png)

5.  Click on the **Save** button. You will be returned to the list of users,
and the service account ID should be listed with the chosen user role(s).

6.  Repeat the process for all other service accounts you want to add.

7.  View existing service account users by going to the Users tab. These
    will be users with an email address ending in 'gserviceaccount.com'.
  1. Giờ đây, bạn có thể truy cập vào tài khoản Manufacturer Center bằng cách sử dụng tài khoản dịch vụ bằng cách sử dụng quy trình Thông tin xác thực mặc định của ứng dụng Google hoặc bằng cách trực tiếp sử dụng quy trình tài khoản dịch vụ. Mẫu API Manufacturer Center cho thấy cách sử dụng cả hai quy trình cho thông tin xác thực tài khoản dịch vụ trong mỗi ngôn ngữ lập trình được hỗ trợ. Xem các mã mẫu để thử tài khoản dịch vụ mới và tìm hiểu những thay đổi bạn sẽ cần để sử dụng tài khoản dịch vụ trong mã của riêng bạn.

Câu hỏi thường gặp

Tôi có thể đăng nhập vào giao diện người dùng web của Manufacturer Center bằng tài khoản dịch vụ của mình không?

Không, tài khoản dịch vụ không phải là Tài khoản Google thông thường và không thể truy cập vào giao diện người dùng web của Manufacturer Center.

Tôi cần làm mới mã truy cập tài khoản dịch vụ bao lâu một lần?

Mã thông báo truy cập sẽ hết hạn sau một giờ sau khi được Máy chủ uỷ quyền Google OAuth 2.0 phát hành. Khi một mã truy cập hết hạn, ứng dụng phải sử dụng thư viện ứng dụng để tìm nạp một mã truy cập khác.