【问题标题】:Getting access token and refresh token without asking for user permission在不请求用户许可的情况下获取访问令牌和刷新令牌
【发布时间】:2018-08-23 21:32:25
【问题描述】:

我想访问 Google Drive 以使用 Google.Apis.Drive.v3 上传文件。 这是我的代码:

protected async void btnConnectGoogleDrive_Click(object sender, EventArgs e)
{
    string[] Scopes = { DriveService.Scope.Drive };
    UserCredential credential;
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "",
                ClientSecret = ""
            },
            Scopes,
            "user",
            CancellationToken.None);

    InsertGoogleDriveToken(credential.Token.AccessToken, areaID, "accesstoken");
    InsertGoogleDriveToken(credential.Token.RefreshToken, areaID, "refreshtoken");

    // Create Drive API service.
    var service = new DriveService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "SocialLadder",
    });

    GoogleDriveUpload(service, areaID);
}

在这里,我可以获得 AccessToken 和 RefreshToken,但用户没有被重定向到权限页面,所以当我尝试上传图像以驱动时,它给了我“permission_not_granted”的错误。在 MVC 中使用相同的东西,效果很好。

请帮我解决这个问题。

【问题讨论】:

  • 您使用的是服务帐号还是普通用户帐号?
  • 我使用的是普通用户帐号

标签: c# google-api google-drive-api google-oauth google-api-dotnet-client


【解决方案1】:

我怀疑您更改了范围。您需要对用户进行身份验证。执行以下操作之一

  • 转到 %appdata% 并删除此用户的 creditlas 文件。
  • 将“user”更改为“user1”

您的应用程序应要求用户再次进行身份验证。

【讨论】:

  • %appdata% 中没有文件。但是将“用户”更改为“用户1”就像魅力一样。我能知道什么是“用户”吗?如果我使用 useremail 而不是 "user" 会更好吗??
  • 文件数据存储存储由您发送给它的用户表示的凭据文件my blog post here 电子邮件是个好主意或用户ID
  • 非常感谢@DalmTo 的帮助。
  • 还有一个问题,如果我想在数据库中保存访问令牌和刷新令牌而不是将它们存储在文件中怎么办?基本上我想做的是我想运行一个后台作业,每天将新的图像从服务器上传到谷歌驱动器。所以我不希望用户每次都进行身份验证。
猜你喜欢
  • 1970-01-01
  • 2016-06-15
  • 2013-08-15
  • 2015-07-19
  • 1970-01-01
  • 2022-11-17
  • 2012-10-25
  • 2018-10-14
  • 2022-01-23
相关资源
最近更新 更多