Re: Re: OAuth 2.0 token

Devi Prasad Chinni <[email protected]>
Newsgroups gmane.comp.web.blogger.api
Message-ID <[email protected]>
I want this code in c# any one please help me..

On Tuesday, June 16, 2015 at 6:26:42 AM UTC+5:30, James Bryant wrote:
>
> Thanks!
>
> On Sunday, June 14, 2015 at 7:45:24 PM UTC-5, Gramps Gramps wrote:
>>
>> HI, sure! :)
>>
>>  
>>
>> HEre it is (have fun):
>>
>>  
>>
>>  
>>
>> package org.gizmo;
>>
>>  
>>
>> import java.util.Arrays;
>>
>> import java.util.Scanner;
>>
>>  
>>
>> import com.google.api.client.auth.oauth2.AuthorizationCodeFlow;
>>
>> import com.google.api.client.auth.oauth2.AuthorizationCodeRequestUrl;
>>
>> import com.google.api.client.auth.oauth2.AuthorizationCodeTokenRequest;
>>
>> import com.google.api.client.auth.oauth2.RefreshTokenRequest;
>>
>> import com.google.api.client.auth.oauth2.TokenResponse;
>>
>> import 
>> com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
>>
>> import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
>>
>> import com.google.api.client.http.BasicAuthentication;
>>
>> import com.google.api.client.http.GenericUrl;
>>
>> import com.google.api.client.http.HttpTransport;
>>
>> import com.google.api.client.http.javanet.NetHttpTransport;
>>
>> import com.google.api.client.json.JsonFactory;
>>
>> import com.google.api.client.json.jackson2.JacksonFactory;
>>
>>  
>>
>> public class GoogleOauth2Authentication
>>
>> {
>>
>>  
>>
>>     /**
>>
>>      * This method performs OAuth2 authentication on Google Servers
>>
>>      * 
>>
>>      * @return
>>
>>      */
>>
>>     public static GoogleCredential GoogleOauth2Authentication(String 
>> googleServiceScope, String googleRefreshKey, String googleRefreshKeyFile)
>>
>>     {
>>
>>         String GOOGLE_REFRESH_TOKEN = "";
>>
>>         String GOOGLE_ACCESS_TOKEN = "";
>>
>>         
>>
>>         try
>>
>>         {
>>
>>         
>>
>>         HttpTransport httpTransport = new NetHttpTransport();
>>
>>         JsonFactory jsonFactory = new JacksonFactory();
>>
>>         TokenResponse tokenResponse = new TokenResponse();
>>
>>  
>>
>>         //Check of we have a previous Refresh Token cached
>>
>>         if (googleRefreshKey.length() == 0)
>>
>>             {
>>
>>             //No Google OAuth2 Key has been previously cached
>>
>>         
>>
>>             //Request the user to grant access to the 
>> Picasa(Blogger/other Resource (uses the Google Authentication servers)
>>
>>             AuthorizationCodeFlow.Builder codeFlowBuilder = 
>>
>>                             new 
>> GoogleAuthorizationCodeFlow.Builder(httpTransport, 
>>
>>                                     jsonFactory, 
>>
>>                                     Config.GOOGLE_CLIENT_ID, 
>>
>>                                     Config.GOOGLE_CLIENT_SECRET, 
>>
>>                                     Arrays.asList(googleServiceScope));
>>
>>             
>>
>>             AuthorizationCodeFlow codeFlow = codeFlowBuilder.build();
>>
>>             AuthorizationCodeRequestUrl authorizationUrl = 
>> codeFlow.newAuthorizationUrl();
>>
>>             authorizationUrl.setRedirectUri(Config.GOOGLE_REDIRECT_URI);
>>
>>             
>>
>>             System.out.println("Hi, you need to grant access to the 
>> Google Service: " + googleServiceScope + "\n");
>>
>>             System.out.println("Go to the following address (using your 
>> web browser):\n" + authorizationUrl);
>>
>>             System.out.println("Login using the account which you use to 
>> access the service and 'Grance Access'");
>>
>>             System.out.print("You will get a code, just copy+paste it in 
>> here:>> ");
>>
>>             String code = new Scanner(System.in).nextLine();
>>
>>             System.in.close();
>>
>>             
>>
>>             //Use the code returned by the Google Authentication Server 
>> to generate an Access Code
>>
>>             AuthorizationCodeTokenRequest tokenRequest = 
>> codeFlow.newTokenRequest(code);
>>
>>             tokenRequest.setRedirectUri(Config.GOOGLE_REDIRECT_URI);
>>
>>             tokenResponse = tokenRequest.execute();
>>
>>             
>>
>>             GOOGLE_REFRESH_TOKEN = tokenResponse.getRefreshToken();
>>
>>             GOOGLE_ACCESS_TOKEN = tokenResponse.getAccessToken();
>>
>>             
>>
>>             //Store the Refresh Token for later usage (this avoid having 
>> to request the user to 
>>
>>             //Grant access to the application via the webbrowser again
>>
>>             GizmoUtil.saveFile(googleRefreshKeyFile, 
>> GOOGLE_REFRESH_TOKEN);
>>
>>             
>>
>>  
>>
>>             }
>>
>>         else
>>
>>             {
>>
>>             //There is a Google OAuth2 Key cached previously.
>>
>>             //Use the refresh token to get a new Access Token
>>
>>             
>>
>>             //Get the cached Refresh Token
>>
>>             GOOGLE_REFRESH_TOKEN = new String(googleRefreshKey);
>>
>>             
>>
>>             //Now we need to get a new Access Token using our previously 
>> cached Refresh Token
>>
>>             RefreshTokenRequest refreshTokenRequest = new 
>> RefreshTokenRequest(httpTransport, 
>>
>>                                                              jsonFactory, 
>>
>>                                                              new 
>> GenericUrl(Config.GOOGLE_TOKEN_SERVER_URL), 
>>
>>
>>                                                              GOOGLE_REFRESH_TOKEN);
>>
>>             
>>
>>             refreshTokenRequest.setClientAuthentication(new 
>> BasicAuthentication(Config.GOOGLE_CLIENT_ID, Config.GOOGLE_CLIENT_SECRET));
>>
>>             
>> refreshTokenRequest.setScopes(Arrays.asList(googleServiceScope));
>>
>>             
>>
>>             tokenResponse = refreshTokenRequest.execute();
>>
>>             
>>
>>             //Get and set the Refresn the Access Tokens
>>
>>             GOOGLE_ACCESS_TOKEN = new 
>> String(tokenResponse.getAccessToken());
>>
>>             }
>>
>>  
>>
>>         //At this point we have a valid Google Access Token
>>
>>         //Let us access Picasa then!
>>
>>         GoogleCredential credential = new GoogleCredential.Builder()
>>
>>                 .setClientSecrets(Config.GOOGLE_CLIENT_ID, 
>> Config.GOOGLE_CLIENT_SECRET)
>>
>>                 .setJsonFactory(jsonFactory)
>>
>>                 .setTransport(httpTransport)
>>
>>                 .build()
>>
>>                 .setAccessToken(GOOGLE_ACCESS_TOKEN)
>>
>>                 .setRefreshToken(GOOGLE_REFRESH_TOKEN);
>>
>>         
>>
>>         //Return the credential object
>>
>>         return credential;
>>
>>         }
>>
>>         catch (Exception e)
>>
>>             {
>>
>>             return null;
>>
>>             }  
>>
>>     }
>>
>> }.
>>
>

-- 
You received this message because you are subscribed to the Google Groups "Blogger Developer Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/bloggerdev.
For more options, visit https://groups.google.com/d/optout.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.