Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Application Flow WIKI Example error #215

Open
Joben28 opened this issue May 27, 2019 · 1 comment
Open

Application Flow WIKI Example error #215

Joben28 opened this issue May 27, 2019 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@Joben28
Copy link

Joben28 commented May 27, 2019

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
Copy link
Member

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

@christopherseeley christopherseeley self-assigned this Jun 11, 2019
@christopherseeley christopherseeley added the bug Something isn't working label Jun 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants