googleads / googleads-dotnet-lib

Google Ad Manager SOAP API .NET client library
Apache License 2.0
109 stars 199 forks source link

Application Flow WIKI Example error #215

Open Joben28 opened 5 years ago

Joben28 commented 5 years ago

I think the following Wiki Page has some inaccuracy in it's code examples

Wiki Page: https://github.com/googleads/googleads-dotnet-lib/wiki/API-access-using-own-credentials-(installed-application-flow)

The following code is used in the example for fetching refresh tokens in a .NET console application.

private static void DoAuth2Authorization(AdWordsUser user) {
    // Since we are using a console application, set the callback url to null.
    user.Config.OAuth2RedirectUri = null;
    AdsOAuthProviderForApplications oAuth2Provider =
        (user.OAuthProvider as AdsOAuthProviderForApplications);
    // Get the authorization url.
    string authorizationUrl = oAuth2Provider.GetAuthorizationUrl();
    Console.WriteLine("Open a fresh web browser and navigate to \n\n{0}\n\n. You will be " +
        "prompted to login and then authorize this application to make calls to the " +
        "AdWords API. Once approved, you will be presented with an authorization code.",
        authorizationUrl);

    // Accept the OAuth2 authorization code from the user.
    Console.Write("Enter the authorization code :");
    string authorizationCode = Console.ReadLine();

    // Fetch the access and refresh tokens.
    oAuth2Provider.FetchAccessAndRefreshTokens(authorizationCode);
  }
}

The issue is that the example says to

Set the callback url to null

but this caused the application to throw this error when running the application:

Value cannot be null.
Parameter name: Looks like your application is not configured to use OAuth2 properly. Required OAuth2 parameter RedirectUri is missing. You may run Common\\Utils\\OAuth2TokenGenerator.cs to generate a default OAuth2 configuration.

I have discovered that rather than setting user.Config.OAuth2RedirectUri = null, you avoid this issue entirely by just not setting the uri at all.

The working code example

private static void DoAuth2Authorization(AdWordsUser user)
        {
            // Since we are using a console application, set the callback url to null.
           // Dont do this - > user.Config.OAuth2RedirectUri = null;
            AdsOAuthProviderForApplications oAuth2Provider =
                (user.OAuthProvider as AdsOAuthProviderForApplications);
            // Get the authorization url.
            string authorizationUrl = oAuth2Provider.GetAuthorizationUrl();
            Console.WriteLine("Open a fresh web browser and navigate to \n\n{0}\n\n. You will be " +
                "prompted to login and then authorize this application to make calls to the " +
                "AdWords API. Once approved, you will be presented with an authorization code.",
                authorizationUrl);

            // Accept the OAuth2 authorization code from the user.
            Console.Write("Enter the authorization code :");
            string authorizationCode = Console.ReadLine();

            // Fetch the access and refresh tokens.
            oAuth2Provider.FetchAccessAndRefreshTokens(authorizationCode);
        }

Either I was misunderstanding something, or this wiki documentation is inaccurate and caused a huge headache.

I hope this is the appropriate place to raise the issue.

christopherseeley commented 5 years ago

Thanks for pointing this out. I updated the wiki to remove these lines. We also need to update the example code.

For reference, here's the OAuth2 docs on redirect URIs and installed applications: https://developers.google.com/identity/protocols/OAuth2InstalledApp#step-2-send-a-request-to-googles-oauth-20-server