Google.Apis.Requests.RequestError Request had insu

2019-08-27 18:24发布

问题:

i am try to create a new project using resourcemanager.projects.create But got an error like :(

Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
    Message[Request had insufficient authentication scopes.] Location[ - ] Reason[forbidden] Domain[global]
]

Can anyone please tell me What i am doing wrong. Here is my code :

private async void GoogleClick(object sender, RoutedEventArgs e)
{
    try
    {
        var cr = new PromptCodeReceiver();

        var result = await GoogleWebAuthorizationBroker.AuthorizeAsync(
        new ClientSecrets { ClientId = GoogleClientId,ClientSecret = GoogleSecretKey },
            new[] { "email", "profile" },
                "user",
                 CancellationToken.None);

        if (result.Token.IsExpired(SystemClock.Default))
        {
            await result.RefreshTokenAsync(CancellationToken.None);
        }

        CloudResourceManagerService cloudResourceManagerService = new CloudResourceManagerService(new BaseClientService.Initializer
        {
            HttpClientInitializer = GetCredential(result.Token.AccessToken, FirebaseAuthType.Google),
            ApplicationName = "Kapiling",
            //ApiKey = "apikey"
        });

        // TODO: Assign values to desired properties of `requestBody`:
        Data.Project requestBody = new Data.Project();
        requestBody.Name = "TESTING";
        requestBody.ProjectNumber = 415104041262;
        requestBody.ProjectId = "tokyo-rain-123";
        requestBody.CreateTime = "2014-10-02T15:01:23.045123456Z";

        ProjectsResource.CreateRequest request = cloudResourceManagerService.Projects.Create(requestBody);

}

I am try to access using public static GoogleCredential FromAccessToken(string accessToken, IAccessMethod accessMethod = null); method

public static GoogleCredential GetCredential(string accessToken, FirebaseAuthType authType)
    {
        GoogleCredential credential = GoogleCredential.FromAccessToken(accessToken, null);
        return credential;
    }

Thanks for everyone who help me i Solved this issues. Thanks again. :)

回答1:

From looking at the GCP documentation, it seems like you (also) need the https://www.googleapis.com/auth/cloud-platform scope. See https://cloud.google.com/resource-manager/docs/authorizing



回答2:

Change your code for GetCredential(). Note: I am not sure what you are trying to do with FirebaseAuthType so I have left authType as an unused parameter.

public static GoogleCredential GetCredential(string accessToken, FirebaseAuthType authType)
    {
        GoogleCredential credential = GoogleCredential.FromAccessToken(accessToken, null);
// The following line is no longer needed
//      credential = credential.CreateScoped("https://www.googleapis.com/auth/cloud-platform");
        return credential;
    }

[EDIT]: Additional code to change due to the comment createScopedRequired = false

You received this message because you already authenticated with scopes. To change the scopes requires another authorization. In this case request the correct scopes. Note, you do not need the other scopes (email and profile). Put them back in later if you do.

string[] scopes = new string[] { "https://www.googleapis.com/auth/cloud-platform" };

var result = GoogleWebAuthorizationBroker.AuthorizeAsync(
    new ClientSecrets {
        ClientId = GoogleClientId,
        ClientSecret = GoogleSecretKey },
    scopes,
    "user",
    CancellationToken.None).Result;