c#: google drive : google apis.services are you missing an assembly or reference

一世执手 提交于 2020-01-20 07:19:24

问题


i am trying to use google.drive for .net using the quickstart example. i have installed dlls via nuget, but am receiving the following error that i am missing a reference or assembly for google.apis.service. any help would be appreciated

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:


  1. Try to run our Drive.Sample from our samples repository - https://code.google.com/p/google-api-dotnet-client/source/browse/?repo=samples#hg%2FDrive.Sample. You can see a working implementation there (it also includes media upload and download code).

  2. Try to start over again and install the following two packages (I'm not sure if you already did so): Google.Apis.Drive.v2 - https://www.nuget.org/packages/Google.Apis.Drive.v2/ Google.Apis.Authetnication - https://www.nuget.org/packages/Google.Apis.Authentication

UPDATE (January 22nd 2014):

We reimplemented the OAuth 2.0 library and removed the dependency in DotNetOpenAuth several months ago. You can read more about it in the announcements blog. Search specifically for the 1.6.0-beta release.

You should also take a look in our OAuth 2.0 page - https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2.

Another good reference is our samples repository. All the samples had been upgraded to use the new Google.Apis.Auth package.



来源:https://stackoverflow.com/questions/18897662/c-google-drive-google-apis-services-are-you-missing-an-assembly-or-reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!