Google Sign-In for Server-Side Apps for .NET

1,528 views
Skip to first unread message

Rick Chang

unread,
Jan 31, 2016, 4:54:01 PM1/31/16
to google-api-dotnet-client
Hi, We are trying to use the .net library to do the "google sign-in for server-side app". I found an article on Google which matches what we wanted to do. The example is in Java and I couldn't figure out how to translate it using the Google .NET client library. Please help.

Java code: (https://developers.google.com/identity/sign-in/web/server-side-flow)
--------------------------------------------------------
// (Receive authCode via HTTPS POST)

// Set path to the Web application client_secret_*.json file you downloaded from the
// Google Developers Console: https://console.developers.google.com/project/_/apiui/credential
// You can also find your Web application client ID and client secret from the
// console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest
// object.
String CLIENT_SECRET_FILE = "/path/to/client_secret.json";

// Exchange auth code for access token
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(
JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE));
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
"https://www.googleapis.com/oauth2/v4/token",
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
authCode,
REDIRECT_URI) // Specify the same redirect URI that you use with your web
// app. If you don't have a web version of your app, you can
// specify an empty string.
.execute();

String accessToken = tokenResponse.getAccessToken();

// Use access token to call API
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
Drive drive =
new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
.setApplicationName("Auth Code Exchange Demo")
.build();
File file = drive.files().get("appfolder").execute();

// Get profile info from ID token
GoogleIdToken idToken = tokenResponse.parseIdToken();
GoogleIdToken.Payload payload = idToken.getPayload();
String userId = payload.getSubject(); // Use this value as a key to identify a user.
String email = payload.getEmail();
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");

Matthew Riley

unread,
Feb 9, 2016, 2:13:14 PM2/9/16
to google-api-d...@googlegroups.com
Hi Rick,

If you're to this point in the example, you've already sent the user to a consent page with your client ID and client secret and received an authorization code. To turn that authorization code into an access token, you'll use the AuthorizationCodeFlow.ExchangeCodeForTokenAsync method to get a TokenResponse, from which you can create a UserCredential.


If you're using ASP.NET MVC, the code can be easier. I'd encourage you to look at Google's .NET authentication samples.

All that said, I apologize that our documentation isn't clearer for this scenario.

Cheers,
Matt


--
You received this message because you are subscribed to the Google Groups "google-api-dotnet-client" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-api-dotnet-...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages