c#:google drive:google apis.services你错过了程序集或引用

时间:2013-09-19 14:37:37

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

我正在尝试使用google.drive for .net使用quickstart示例。我已经通过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);
        }
    }

1 个答案:

答案 0 :(得分:0)

  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

  3. 更新(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包。