【问题标题】:Google Picker Client with Server .Net File Download 403 Forbidden带有服务器的 Google Picker 客户端 .Net 文件下载 403 被禁止
【发布时间】:2017-01-26 06:43:28
【问题描述】:

我在网络客户端中使用 Google 选择器来允许用户授权我的应用程序并选择要下载的文件。我检索所选文件的 fileId 和 oauthToken 并将其传递到我的后端,类似于我在此处找到的 (Google picker and backend file download)。

后端进程是 .Net,我正在使用下面显示的代码提交文件请求。但是,当我发送相同的 Url、FileId 和 oAuthToken 信息时,即使相同的 Get 请求在 Postman 中工作正常,我也会收到 403 Forbidden 错误。

关于我的 HttpWebRequest 设置可能有什么问题有什么想法吗?

public void Download(string pOAuthToken, string pFileId, string pFileName) {
    HttpWebRequest request;
    HttpWebResponse response;

    bool result = false;
    string url = "";

    try {
        url = "https://www.googleapis.com/drive/v3/files/" + pFileId + "?alt=media";
        request = WebRequest.Create(url);
        request.Headers.Add("Authorization", ("Bearer " + pOAuthToken));

        response = request.GetResponse();

        //  Insert code to download file here

        result = true;
    }
    catch (Exception ex) {
        LogError("Download exception.", ex);
    }
    finally {
        response.Close();
    }

    return result;
}

【问题讨论】:

  • 403 一般表示权限。响应正文中应该有更多信息。
  • 您是否尝试使用webContentLink 下载文件?我可以使用它从我的云端硬盘下载文件。
  • 响应正文不包含任何其他信息。我尝试将范围权限从 auth/drive.readonly 增加到 auth/drive 但这没有效果。在 google.picker 文档中,我找不到 webContentLink 属性。最接近的属性是 downloadUrl,但该属性在传递回回调函数的 google.picker 文档中未定义。

标签: .net google-drive-api httpwebrequest google-picker


【解决方案1】:

我能够通过使用 Google Drive REST API 版本 2 解决该问题:

url = "https://www.googleapis.com/drive/v2/files/" + pFileId + "?alt=media";

文档表明这也适用于版本 3 (https://developers.google.com/drive/v3/web/manage-downloads),但不适用于我的情况。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    相关资源
    最近更新 更多