【问题标题】:c#: google drive : google apis.services are you missing an assembly or referencec#: google drive : google apis.services 您是否缺少程序集或参考
【发布时间】:2013-09-19 14:37:36
【问题描述】:

我正在尝试使用快速入门示例将 google.drive 用于 .net。我已经通过 nuget 安装了 dll,但收到以下错误,我缺少 google.apis.service 的引用或程序集。任何帮助将不胜感激

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DotNetOpenAuth.OAuth2;
using Google.Apis.Authentication.OAuth2;
using Google.Apis.Authentication.OAuth2.DotNetOpenAuth;
using Google.Apis.Drive.v2;
using Google.Apis.Drive.v2.Data;
using Google.Apis.Util;
using Google.Apis.Services;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {

         String CLIENT_ID = "some_id";
            String CLIENT_SECRET = "some_secret";

            // Register the authenticator and create the service
            var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET);
            var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization);
            var service = new DriveService(new BaseClientService.Initializer()
            {
                Authenticator = auth
            });

            File body = new File();
            body.Title = "My document";
            body.Description = "A test document";
            body.MimeType = "text/plain";

            byte[] byteArray = System.IO.File.ReadAllBytes("document.txt");
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

            FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, "text/plain");
            request.Upload();

            File file = request.ResponseBody;
            Console.WriteLine("File id: " + file.Id);
            Console.WriteLine("Press Enter to end this process.");
            Console.ReadLine();
        }

        private static IAuthorizationState GetAuthorization(NativeApplicationClient arg)
        {
            // Get the auth URL:
            IAuthorizationState state = new AuthorizationState(new[] { DriveService.Scopes.Drive.GetStringValue() });
            state.Callback = new Uri(NativeApplicationClient.OutOfBandCallbackUrl);
            Uri authUri = arg.RequestUserAuthorization(state);

            // Request authorization from the user (by opening a browser window):
            Process.Start(authUri.ToString());
            Console.Write("  Authorization Code: ");
            string authCode = Console.ReadLine();
            Console.WriteLine();

            // Retrieve the access token by using the authorization code:
            return arg.ProcessUserAuthorization(authCode, state);
        }
    }

【问题讨论】:

  • 不要把秘密的东西公开!
  • 你在什么时候得到异常?还是在编译期间?

标签: c# .net google-api google-drive-api google-api-dotnet-client


【解决方案1】:
  1. 尝试从我们的示例存储库 - https://code.google.com/p/google-api-dotnet-client/source/browse/?repo=samples#hg%2FDrive.Sample 运行我们的 Drive.Sample。您可以在那里看到一个有效的实现(它还包括媒体上传和下载代码)。

  2. 尝试重新开始并安装以下两个软件包(我不确定您是否已经这样做了): Google.Apis.Drive.v2 - https://www.nuget.org/packages/Google.Apis.Drive.v2/ Google.Apis.Authetnication - https://www.nuget.org/packages/Google.Apis.Authentication

更新(2014 年 1 月 22 日):

几个月前,我们重新实现了 OAuth 2.0 库并删除了 DotNetOpenAuth 中的依赖项。 您可以在announcements blog 中阅读更多相关信息。专门搜索 1.6.0-beta 版本。

您还应该查看我们的 OAuth 2.0 页面 - https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2

另一个很好的参考是我们的示例存储库。所有示例都已升级为使用新的 Google.Apis.Auth 包。

【讨论】:

  • 我遇到了同样的问题,但是这里提供的所有三个链接似乎都有问题。由于似乎存在 ssl 证书错误,下载 zip 被阻止(无法连接到真实的 samples.google-api-dotnet-client.googlecode.com 目前正在干扰您与 samples.google-api-dotnet-client 的安全连接.googlecode.com。)
  • 至于 Nuget,这两个包名称都已更改,我可以使用命令“get-package -listavailable google.apis”查看可用的包,发现它只是更改为“Google.Apis.Drive”,据我所知无处记录。至于搜索可用包时的身份验证,名称列是字符限制的,所以我返回“Google.Apis.Authentication....”作为包名。所以我留下了一些字符,无法找出新的包名是什么!有没有办法可以找出他们将 pkg 名称更改为什么?
  • @peleyal “所有样本都已升级为使用新的 Google.Apis.Auth 包”。我可以知道这些在哪里吗?我刚刚从 GitHub 下载了最新的 DrEdit,它仍在使用旧的 Google.Apis.Authentication 命名空间。
猜你喜欢
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 2016-10-08
  • 2010-10-01
  • 2014-02-16
  • 2014-10-05
  • 2014-05-07
  • 1970-01-01
相关资源
最近更新 更多