1
votes

I am trying to make a web application is ASP.NET MVC 5 with which I can authenticate a user with a Google Account and then read data from his/her spreadsheets stored in Google Drive/Google Sheets.

I am using Google API to authenticate a user. After a user is successfully authenticated, I get the credentials back from Google in an object which is of type Google.Apis.Auth.OAuth2.Web AuthResult.UserCredential

I can then successfully create a service to list files from Drive using code similar to

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

Now, I want to use GData API to read content from spreadsheets in Drive. For this to work, I need to have a SpreadsheetsService object and then set it's RequestFactory parameter to an instance of GOAuth2RequestFactory and this in turn needs OAuth2 parameters to be specified in an instance of class OAuth2Parameters.

How can I reuse the credentials obtained using the Google Api in GData API?

2
why not just create a SpreadsheetsService ? as long as the scopes used in creating your credential allows for both drive and SpreadsheetsService there is no reason you cant create both. - DaImTo
I have created a SpreadsheetsService. I wouldn't want to start the authentication cycle again. I am looking for a way to use the Credentials from API in GData - Tharunavignesh J
Don't think I understand the problem creating a new service wont start authentication again you aready have result.credentials just use it in both. - DaImTo
@DaImTo The credentials that we had obtained from the Google API doesn't work with GData(since GData is an old API). Please see the image. I took a grab from Visual Studio pbrd.co/10uYsD7 - Tharunavignesh J
How did you create these credentials? are they old open id credentials or something? Can you show the code for authentication? - DaImTo

2 Answers

1
votes

I am already doing the thing you want to do,

Code for how I passed the GData tokens Issue with OAuth2 authentication with google spreadsheet

i.e. I use a single OAuth2 access/refresh token set. Using the same tokens for both gdata calls & drive API calls.

0
votes

This is the code that finally worked for me

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; } }

        }

And then, in the controller, OAuth2 parameters were copied to 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;