.NET 適用的 Google API 用戶端程式庫會使用 client_secrets.json
檔案儲存 client_id
、client_secret
和其他 OAuth 2.0 參數。
client_secrets.json
檔案是 JSON 格式的檔案,內含用戶端 ID、用戶端密鑰和其他 OAuth 2.0 參數。以下是網頁應用程式的 client_secrets.json
檔案範例:
{ "web": { "client_id": "asdfjasdljfasdkjf", "client_secret": "1912308409123890", "redirect_uris": ["https://www.example.com/oauth2callback"], "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token" } }
以下是已安裝應用程式的 client_secrets.json
範例檔案:
{ "installed": { "client_id": "837647042410-75ifg...usercontent.com", "client_secret":"asdlkfjaskd", "redirect_uris": ["http://localhost"], "auth_uri": "https://accounts.google.com/o/oauth2/auth", "token_uri": "https://accounts.google.com/o/oauth2/token" } }
格式會定義以下兩種用戶端 ID 類型的其中之一:
web
:網頁應用程式。installed
:已安裝的應用程式。
web
和 installed
子物件有下列必要成員:
-
client_id
(字串):用戶端 ID。 -
client_secret
(字串):用戶端密鑰。
這個檔案的所有其他成員皆為選用,且 .NET 用戶端程式庫不會使用這些成員。
動機
傳統上,OAuth 2.0 端點的提供者預期使用其服務從註冊頁面複製用戶端 ID 和用戶端密鑰,並貼到可運作的程式碼中。這個方法容易發生錯誤,且僅針對讓 OAuth 2.0 正常運作所需的資訊提供不完整的資訊。(OAuth 2.0 也必須瞭解所有端點,並設定重新導向 URI)。如果服務供應商提供可下載的 client_secrets.json
檔案,而用戶端程式庫也準備好取用這些檔案,那麼實作 OAuth 2.0 會比較簡單,也較不容易出錯。