C# 呼叫 Google Cloud Functions
進到GCP平台後
點選"API和服務" > "憑證" > "建立憑證"

幫服務帳戶取名

點選"請選擇角色"

※注意此步驟是關鍵※ 點選"請選擇角色"後 接著找到 "Cloud Functions" 然後選擇 "Cloud Functions 叫用者"

選擇 "Cloud Functions 叫用者" 後點選"繼續"

點選"完成"

在畫面上的服務帳戶清單中點選要編輯的帳戶

點選 "新增金鑰" > "建立新的金鑰"

金鑰類型選擇"JSON"

取得"秘密金鑰檔案" (這個檔案要放在待會要使用的程式的根目錄)

點選 本次要使用的函式

點選"編輯"

取得 觸發網址 audience
(於下方範例程式中使用)

點選"下一步" > 編輯完程式後點選"部署"
使用Google OAuth套件
using Google.Apis.Auth.OAuth2;
範例程式
string rootPath = Directory.GetCurrentDirectory();
string jsonfileName = Path.Combine(rootPath, "[私密金鑰檔案名稱].json");
GoogleCredential credential = GoogleCredential.FromFile(jsonfileName);
var audience = "https://asia-east1-xxxxxxx-xxxxx.cloudfunctions.net/mytest";
var openIdConnToken = credential.GetOidcTokenAsync(OidcTokenOptions.FromTargetAudience(audience), CancellationToken.None).Result;
string accessToken = openIdConnToken.GetAccessTokenAsync(CancellationToken.None).Result;
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
HttpResponseMessage httpResponse = httpClient.GetAsync(audience).Result;
if (httpResponse.IsSuccessStatusCode)
{
string responseBody = httpResponse.Content.ReadAsStringAsync().Result;
}