【问题标题】:Reusing Google API credentials in GData API在 GData API 中重用 Google API 凭据
【发布时间】:2014-12-22 12:58:25
【问题描述】:

我正在尝试使用 ASP.NET MVC 5 制作一个 Web 应用程序,我可以使用它对具有 Google 帐户的用户进行身份验证,然后从他/她存储在 Google Drive/Google 表格中的电子表格中读取数据。

我正在使用 Google API 对用户进行身份验证。用户成功通过身份验证后,我从 Google 获取了 Google.Apis.Auth.OAuth2.Web AuthResult.UserCredential 类型的对象中的凭据

然后我可以使用类似于

的代码成功创建一个服务来列出云端硬盘中的文件

var driveService = new DriveService(new BaseClientService.Initializer { HttpClientInitializer = result.Credential, ApplicationName = "ASP.NET MVC Sample" });

现在,我想使用 GData API 从云端硬盘中的电子表格中读取内容。为此,我需要有一个SpreadsheetsService 对象,然后将其RequestFactory 参数设置为GOAuth2RequestFactory 的实例,而这又需要在OAuth2Parameters 类的实例中指定OAuth2 参数。

如何在 GData API 中重复使用使用 Google Api 获得的凭据?

【问题讨论】:

  • 为什么不直接创建一个 SpreadsheetsService 呢?只要创建凭据时使用的范围允许 drive 和 SpreadsheetsService ,就没有理由不能同时创建两者。
  • 我创建了一个SpreadsheetsService。我不想再次开始身份验证周期。我正在寻找一种在 GData 中使用来自 API 的凭据的方法
  • 不要以为我理解创建新服务不会再次启动身份验证的问题,你已经有了结果。凭据只是在两者中都使用它。
  • @DaImTo 我们从 Google API 获得的凭据不适用于 GData(因为 GData 是旧 API)。请看图片。我从 Visual Studio pbrd.co/10uYsD7
  • 您是如何创建这些凭据的?他们是旧的开放身份凭证还是什么?可以出示验证码吗?

标签: google-api google-oauth google-spreadsheet-api google-api-dotnet-client google-authentication


【解决方案1】:

我已经在做你想做的事了,

我如何传递 GData 令牌的代码 Issue with OAuth2 authentication with google spreadsheet

即我使用单个 OAuth2 访问/刷新令牌集。对 gdata 调用和驱动 API 调用使用相同的令牌。

【讨论】:

    【解决方案2】:

    这是最终对我有用的代码

    public class AppFlowMetadata : FlowMetadata
            {
                private static readonly IAuthorizationCodeFlow flow =
                    new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                    {
                        ClientSecrets = new ClientSecrets
                        {
                            ClientId = "randomstring.apps.googleusercontent.com",
                            ClientSecret = "shhhhhh!"
                        },
                        Scopes = new[] { DriveService.Scope.Drive, "https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds", "https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile" },
                        DataStore = new FileDataStore("Drive.Api.Auth.Store")
                    });
    
                public override string GetUserId(Controller controller)
                {
    
                    var user = controller.Session["user"];
                    if (user == null)
                    {
                        user = Guid.NewGuid();
                        controller.Session["user"] = user;
                    }
                    return user.ToString();
                }
    
                public override IAuthorizationCodeFlow Flow { get { return flow; } }
    
            }
    

    然后,在控制器中,将 OAuth2 参数复制到 GData

    var result = await new AuthorizationCodeMvcApp(this, new AppFlowMetadata()).
                    AuthorizeAsync(cancellationToken);
    OAuth2Parameters parameters = new OAuth2Parameters();
                    parameters.ClientId = "somestring.apps.googleusercontent.com";
                    parameters.ClientSecret = "shhhhhh!";
                    parameters.Scope = result.Credential.Token.Scope;
                    parameters.AccessToken = result.Credential.Token.AccessToken;
                    parameters.RefreshToken = result.Credential.Token.RefreshToken;
    
                    GOAuth2RequestFactory requestFactory = new GOAuth2RequestFactory(null, "MySpreadsheetIntegration-v1", parameters);
    
                    SpreadsheetsService service = new SpreadsheetsService("MySpreadsheetIntegration-v1");
                    service.RequestFactory = requestFactory;
    

    【讨论】:

      猜你喜欢
      • 2011-09-16
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 2013-06-28
      • 1970-01-01
      • 2021-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多