Google 驱动器 API returns 尝试验证时出现 401

Google Drive API returns 401 when attempting to authenticate

这是一个全新的项目,也是我第一次尝试使用 Google Drive 的 API。当我 运行:

时,我立即收到 401
static void Main(string[] args)
    {
        UserCredential credential;
        using (var filestream = new FileStream("client_secrets.json",
            FileMode.Open, FileAccess.Read))
        {
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(filestream).Secrets,
                new[] { DriveService.Scope.Drive },
                "user",
                CancellationToken.None,
                new FileDataStore("DriveCommandLineSample")).Result;
        }

此代码是从 https://developers.google.com/drive/web/quickstart/quickstart-cs 中逐字复制的。我将 JSON 文件下载为 client_secrets.json 并验证 GoogleClientSecrets.Load(filestream).Secrets 与我在凭证上看到的值相同页。

我已经使用 Firefox 和 IE 进行了测试。我做错了什么?

@saltface 我无法发表评论,所以我 post 在这里。

您想使用哪个API? Drive API 还是 Drive SDK?

401 error 表示您没有识别,您的凭据错误或者您没有启用 Google API Console.

中的 API

你启用api了吗?然后如果是的话...你有没有生成网络密钥?

Google API Console

可以看出

你必须转到左侧菜单 APIs/Auth => APIs .

在那里您需要搜索 'Drive' 并启用 API。完成后,您将需要创建一个凭据。

我可以通过在电子邮件地址下拉列表中选择我的电子邮件地址来使其正常工作同意屏幕页面。 (之前未设置。)

我创建了另一个 Google 项目并将电子邮件地址留空。在我设置电子邮件地址之前,它表现出相同的行为。

我知道这个问题已经被回答了,但只是作为对其他人的说明:

最近,我在尝试设置 Google Drive "push notifications"(webhooks)时收到 401 Unauthorized 错误响应。事实证明,这是因为我的访问令牌已过期!您可以通过 运行 代码示例 here.

重新生成有效令牌