0
votes

I am trying out google drive api's to do basic upload/download/list files. But I am not sure how to get past authentication.

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets{ClientId = clientId, ClientSecret = clientSecret},                                                                                                  
                                                                                                , scopes
                                                                                                , userName
                                                                                                , CancellationToken.None
                                                                                                , new FileDataStore("Drive.Auth.Store")).Result;

I have seen various SO posts and I have tried adding and javascript origin and redirect uri in the google dev console as http://localhost or as http://localhost/google_oauth2/callback. Or is it that the url should be my drive url?

I am a bit confused with redirect uri's. AFAIU, this oauth code will help me access my google drive as my email id is registered in the dev console with this app and secrets are generated. So I don't have a server but just my localhost.

Sorry if this question is silly but I am still learning auth mechanisms. Will be great help if someone could correct me.

1

1 Answers

0
votes

From the Google documentation, it states that for every request your application sends to the Drive API, you must include an authorization token. The token also identifies your application to Google. You can find the details of the authorization process in this link.

Here is a sample code to manage the authorization.

UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        Scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore(credPath, true)).Result;
                    Console.WriteLine("Credential file saved to: " + credPath);
                }

You'll also need to specify the Open URL for your app, and your preferences on how users create and open files with the app. There are two important things to keep in mind for the Open URL:

  • Make sure you give a fully qualified domain name for Open URL, localhost does not work.
  • The URL must belong to you. After the app registration is complete, you will need to verify your ownership of this URL in order to create a Chrome Web Store listing.

About the redirect URI, it is the callback entry point of the app. The redirect URI specified in the request determines how the authorization code is returned to your application. For example, after end user accepts permissions, "redirect URI" has to be called to get back to the app. Furthermore, the redirect URI should be different than the initial entry point of the app.You can read the documentation here.