我們正在更新 Data API,以符合 YouTube 計算 Shorts 觀看次數的方式。
瞭解詳情
實作 OAuth 2.0 授權
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
YouTube Data API 支援 OAuth 2.0 通訊協定,可授權存取私人使用者資料。下列清單說明了一些核心 OAuth 2.0 概念:
-
當使用者首次嘗試使用應用程式中需要登入 Google Account or YouTube account 才能使用的功能時,應用程式會啟動 OAuth 2.0 授權程序。
-
應用程式會將使用者導向至 Google 的授權伺服器。該頁面的連結會指定應用程式針對使用者帳戶要求的存取權 scope
。scope
會指定應用程式在擔任已驗證使用者時,可擷取、插入、更新或刪除的資源。
-
如果使用者同意授權您的應用程式存取這些資源,Google 就會將權杖傳回您的應用程式。視應用程式的類型而定,它會驗證憑證,或交換成不同類型的權杖。
舉例來說,伺服器端網頁應用程式會將傳回的權杖換成存取權杖和更新權杖。存取權杖可讓應用程式代表使用者授權要求,而更新權杖可讓應用程式在原始存取權杖過期時擷取新的存取權杖。
重要事項:如要使用 OAuth 2.0 授權,您必須在 Google API 控制台中取得授權憑證。
詳情請參閱 OAuth 2.0 授權指南。
OAuth 2.0 流程
Google API 支援多種 OAuth 2.0 使用案例:
- 伺服器端網頁應用程式流程支援可安全儲存持續性資訊的網頁應用程式。
- JavaScript 網頁應用程式流程支援在瀏覽器中執行的 JavaScript 應用程式。
- 行動和電腦應用程式流程支援在裝置 (例如手機或電腦) 上安裝的應用程式。
- 電視和受限制的輸入裝置流程支援輸入功能受限的裝置,例如遊戲主機和攝影機。
- 服務帳戶的 OAuth 2.0 流程支援不存取使用者資訊的伺服器對伺服器互動。
不過,YouTube Data API 不支援這個流程。
Since there is no way to link a Service Account to a YouTube account, attempts to authorize requests with this flow will generate a
NoLinkedYouTubeAccount
error.
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-11-05 (世界標準時間)。
[null,null,["上次更新時間:2024-11-05 (世界標準時間)。"],[[["\u003cp\u003eThe YouTube Data API uses the OAuth 2.0 protocol to authorize access to private user data, which is initiated when a user attempts to use features requiring login.\u003c/p\u003e\n"],["\u003cp\u003eDuring authorization, the application requests specific scopes of access, defining which resources it can manage on behalf of the user.\u003c/p\u003e\n"],["\u003cp\u003eUpon user consent, Google issues a token to the application, which may be exchanged for an access token and a refresh token, allowing the application to act on the user's behalf.\u003c/p\u003e\n"],["\u003cp\u003eGoogle APIs support various OAuth 2.0 flows tailored to different application types, such as server-side web apps, JavaScript web apps, mobile and desktop apps, and limited-input devices.\u003c/p\u003e\n"],["\u003cp\u003eWhile OAuth 2.0 includes a service account flow, the YouTube Data API does not support this method, and using it will result in a \u003ccode\u003eNoLinkedYouTubeAccount\u003c/code\u003e error.\u003c/p\u003e\n"]]],["The YouTube Data API uses OAuth 2.0 for user data access. Applications initiate authorization by directing users to Google, specifying the requested access scope. Upon user consent, Google provides a token. Web applications exchange this for access and refresh tokens, enabling authorized requests and token renewal. Various flows exist for different app types, including server-side web apps, JavaScript web apps, mobile/desktop apps, and limited-input devices. Service accounts are not supported for the YouTube Data API. Authorization requires credentials from the Google API Console.\n"],null,["# Implementing OAuth 2.0 Authorization\n\nThe YouTube Data API supports the [OAuth 2.0 protocol](http://oauth.net/2/) for authorizing access to private user data.\n\nThe following list explains some core OAuth 2.0 concepts:\n\n- When a user first attempts to use features in your application that requires the user to be logged in to a Google Account or YouTube account, your application initiates the OAuth 2.0 authorization process.\n\n- Your application directs the user to Google's authorization server. The link to that page specifies the **scope** of access that your application is requesting for the user's account. The **scope** specifies the resources that your application can retrieve, insert, update, or delete when acting as the authenticated user.\n\n- If the user consents to authorize your application to access those resources, Google returns a token to your application. Depending on your application's type, it either validates the token or exchanges it for a different type of token.\n\n For example, a server-side web application exchanges the returned token for an access token and a refresh token. The access token lets the application authorize requests on the user's behalf, and the refresh token lets the application retrieve a new access token when the original access token expires.\n\n**Important:** To use the OAuth 2.0 Authorization, you need to [obtain authorization credentials in the Google API Console](/youtube/registering_an_application).\n\nFor more details, see the [OAuth 2.0 Authorization Guide](https://developers.google.com/identity/protocols/oauth2).\n\nOAuth 2.0 flows\n---------------\n\nGoogle APIs support several OAuth 2.0 use cases:\n\n- The [server-side web apps](/youtube/v3/guides/auth/server-side-web-apps) flow supports web applications that can securely store persistent information.\n- The [JavaScript web apps](/youtube/v3/guides/auth/client-side-web-apps) flow supports JavaScript applications running in a browser.\n- The [mobile and desktop apps](/youtube/v3/guides/auth/installed-apps) flow supports applications installed on a device, such as a phone or computer.\n- The [TVs and limited-input devices](/youtube/v3/guides/auth/devices) flow supports devices with limited input capabilities, such as game consoles and video cameras.\n- The OAuth 2.0 flow for service account flow supports server-to-server interactions that do not access user information. However, the YouTube Data API does not support this flow. Since there is no way to link a Service Account to a YouTube account, attempts to authorize requests with this flow will generate a `NoLinkedYouTubeAccount` error."]]