Method not found: 'Void Google.Apis.Util.Store.FileDataStore..ctor(System.String)'

我的未来我决定 提交于 2019-12-07 10:08:41

问题


I've been stuck at this for days now. I copied the exact codes from google api samples to upload files to Google Drive. Here is the code

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
           new ClientSecrets
           {
               ClientId = ClientId,
               ClientSecret = ClientSecret,
           },
           new[] { DriveService.Scope.Drive,
            DriveService.Scope.DriveFile },
              "user",
              CancellationToken.None,
              new FileDataStore("MyStore")).Result;

But it would throw an exception at runtime: Method not found: 'Void Google.Apis.Util.Store.FileDataStore..ctor(System.String)'. I already added the necessary Google Api dlls.

Or if anyone could suggest a better code for uploading files to Google Drive in a website which implements Server-Side Authorization. Any help would be greatly appreciated.

UPDATE: I changed my code to this

var token = new TokenResponse { RefreshToken = "1/6hnki1x0xOMU4tr5YXNsLgutzbTcRK1M-QOTEuRVxL4" }; 
               var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                    {
                        ClientId = ClientId,
                        ClientSecret = ClientSecret
                    },
                    Scopes = new[] { 
                        DriveService.Scope.Drive, DriveService.Scope.DriveFile
                    }
                }), "user", token);

But it also throws an exception: Method not found: 'Void Google.Apis.Auth.OAuth2.Flows.GoogleAuthorizationCodeFlow..ctor(Initializer). Is the problem with the dlls?


回答1:


Looks like there is a bug in v1.8.2 of Google APIs Client Library. Try using v1.8.1 using nuget with .NET framework 4.




回答2:


It's really weird. All our samples are using the new library (1.8.2), and they work. Maybe it is related to VS2010, or not installing one of the latest .NET 4 patches? (Make sure .NET Framework 4.0 update KB2468871 (http://www.microsoft.com/en-us/download/details.aspx?id=3556) is installed.

The fact that samples run successfully implies that our environment is different. I'm using VS 2012 on Windows 8. What do you use?

Feel free to open a new issue in our issue tracker (https://code.google.com/p/google-api-dotnet-client/issues/list), and we can take it from there.




回答3:


This is what I've done to resolve the issue :

public UserCredential GetRefreshToken(string refreshToken, string clientID, string clientSecret, string[] scopes)
{
    TokenResponse token = new TokenResponse
    {
        RefreshToken = refreshToken
    };

    IAuthorizationCodeFlow flow = new AuthorizationCodeFlow(new AuthorizationCodeFlow.Initializer(Google.Apis.Auth.OAuth2.GoogleAuthConsts.AuthorizationUrl, Google.Apis.Auth.OAuth2.GoogleAuthConsts.TokenUrl)
    {
        ClientSecrets = new ClientSecrets
        {
            ClientId = clientID,
            ClientSecret = clientSecret
        },
        Scopes = scopes
    });

    UserCredential credential = new UserCredential(flow, "me", token);
    try
    {
        bool success = credential.RefreshTokenAsync(CancellationToken.None).Result;
    }
    catch
    {
        throw;
    }
    return credential;
}



回答4:


Had the same problem using Visual Studio 2013 + Update 3 on win 8.1 .NET 4.5 was set as the Target Framework of my Console Project (this is selected by default). The issue was fixed by removing every reference to the google apis. Then switching the Target Framework to ".NET Framework 4" (Select project from solution explorer, right click -> Properties -> Application Page -> Target Framework dropdown) Then installing apis with nuget in the following order:

PM> Install-Package Google.Apis
PM> Install-Package Google.Apis.Auth
PM> Install-Package Google.Apis.Drive.v2


来源:https://stackoverflow.com/questions/24174657/method-not-found-void-google-apis-util-store-filedatastore-ctorsystem-string

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