Are you trying to access one or more of the Google APIs? Are you unable for some reason to use Googles official .net client library? I have a possible solution for you. In this tutorial i am going to share with you a class i called Google Auths simple. It is just that a simple set of classes that will allow you to authenticate to simply authenticate a user using OAuth2 to google and get a access token back. Once you have your access token you can access any of the Google APIs.
Oauth2 step one the consent screen.
[wp_ad_camp_3]
The initial URL to request that the user give you access to there account should look like this.
https://accounts.google.com/o/oauth2/auth?client_id={clientid}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope={scopes}&response_type=code
- The {clientid} is found on the Google developers console. If you don’t know how to create that you can read my Google developers for beginners tutorial series specifically the OAuth2 tutorial. Google Developer Console Oauth2 credentials.
- {scopes} is a comma separated string representing the permissions that you will need. Each API has a different set of scopes available. Each method with in the API may require different scopes. For example: If i want to add events to a users Google Calendar, asking for only read only permission will not allow me to do this I will need to ask for the permission to add events.
This URL is just a HTTP GET so you can place the URL in a browser window. If you open that link in a browser you will see this.:
Step one and a half
When the user clicks accept an authentication code is returned to you. How you get that code is up to you. I normally place the uri in a web browser control then grab it from there.
Step two exchange the authentication code.
The Authentication code is only used once. It is used to get an Access token and a Refresh token. The access token is what you will use to access the Google API, access tokens expire after one hour.
[wp_ad_camp_5]
step three refresh access token
After an hour has gone by the access token will expire you will need to request a new access token. We use the refresh token to get a new access token. You should save the refresh token some place this is what will enable you to access the users account again with out asking them for permission.
Usage
You can use the access token with the API whose scope you requested access for by simply placing access_token= on the end of the request.
https://www.googleapis.com/drive/v2/files?access_token={yourAccessToken}
Wrap up
We cant always use the Google .net client library in every project. Google Auth simple is a quick and dirty way of enabling Google Authentication in your application.
Linda, thanks for these code samples, they are very helpful. I have been struggling with “Step Two” – exchanging the authorization code for the access and refresh tokens. (I get the authorization code from the client/js login code, which works perfectly. But whether I run it on my localhost or test site, I always get Error 400 (Bad Request) at the “dataStream = response.GetResponseStream()” line. Can you suggest any reasons why I am consistently getting this error? Thank you!
Try and post your code on stackoverflow.com
Hi, i do all step as u said, but when i call https://www.googleapis.com/drive/v2/files?access_token={yourAccessToken} ,(with given access token), it gave me unauthorize, what is the problem? i added scope to it :https://www.googleapis.com/auth/analytics
the access token is probably expired, or not authenticated to the correct scope.
I’m developing a mobile application where a user can sign-in using Google. I’m setting up for “Google OAuth consent screen”, where I’ve mentioned the URL of my site along with a logo and the links of privacy policy, and T&C. It is by default taking the scopes: openid, email, and profile. I’ve submitted the request for verification.
I’ve received an e-mail from Google to share the link of web log-in which is not valid in my case. What I’m missing?
Link of the SO question: https://stackoverflow.com/q/53808206/453767
Please ignore my last comment. Google team has approved it when I mentioned that it is an android app not a web application.