Jay Patel
Jay Patel

Reputation: 95

Getting access token and refresh token without asking for user permission

I want to access Google Drive to upload files using Google.Apis.Drive.v3. Here is my code:

protected async void btnConnectGoogleDrive_Click(object sender, EventArgs e)
{
    string[] Scopes = { DriveService.Scope.Drive };
    UserCredential credential;
    credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "",
                ClientSecret = ""
            },
            Scopes,
            "user",
            CancellationToken.None);

    InsertGoogleDriveToken(credential.Token.AccessToken, areaID, "accesstoken");
    InsertGoogleDriveToken(credential.Token.RefreshToken, areaID, "refreshtoken");

    // Create Drive API service.
    var service = new DriveService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "SocialLadder",
    });

    GoogleDriveUpload(service, areaID);
}

Here I am able to get the AccessToken and RefreshToken but user is not redirected to permission page, so when I try to upload images to drive it gives me error that "permission_not_granted". Used same thing in MVC and that works great.

Please help me with this issue.

Upvotes: 1

Views: 318

Answers (1)

Linda Lawton - DaImTo
Linda Lawton - DaImTo

Reputation: 116958

I suspect that you have changed your scopes. You need to authenticate the user. Do one of the following

  • Go to %appdata% and delete the creditlas file for this user.
  • change "user" to something else say "user1"

Your application should then require the user to authenticate again.

Upvotes: 1

Related Questions