How to fetch access token in Google AppEngine (OAuth 2.0) us

    
    
    
    
    package com.login.client ;
    import java.io.BufferedReader ;
    import java.io.ByteArrayInputStream ;
    import java.io.IOException ;
    import java.io.InputStream ;
    import java.io.InputStreamReader ;
    import java.net.MalformedURLException ;
    import java.net.URL ;
    import java.util.List ;
     
    import com.google.appengine.api.urlfetch.HTTPHeader ;
    import com.google.appengine.api.urlfetch.HTTPMethod ;
    import com.google.appengine.api.urlfetch.HTTPRequest ;
    import com.google.appengine.api.urlfetch.HTTPResponse ;
    import com.google.appengine.api.urlfetch.URLFetchService ;
    import com.google.appengine.api.urlfetch.URLFetchServiceFactory ;
     
    public class FetchUrl {
            private static String client_id = "<Client ID>" ;
            private static String client_secret = "<client_secret>" ;
            private static String redirectURL = "http://gfksgnbgfcv.appspot.com/index2.jsp" ;
            private static URLFetchService fetchService ;
            public static String getParseValue ( String code ) {
                    HTTPResponse httpResponse = null ;
                      try {
                      String urlStr = "https://accounts.google.com/o/oauth2/token" ;
                      URL url = new URL (urlStr ) ;
                     HTTPMethod method =HTTPMethod. POST ;
                     HTTPRequest httpRequest = new HTTPRequest (url,method ) ;
                     fetchService = URLFetchServiceFactory. getURLFetchService ( ) ;
                     httpRequest. setHeader ( new HTTPHeader ( "Content-Type", "application/x-www-form-urlencoded" ) ) ;
                     httpRequest. setHeader ( new HTTPHeader ( "Content-length", "250" ) ) ;
                     httpRequest. addHeader ( new HTTPHeader ( "code", code ) ) ;
                     httpRequest. addHeader ( new HTTPHeader ( "client_id", client_id ) ) ;
                     httpRequest. addHeader ( new HTTPHeader ( "client_secret", client_secret ) ) ;
                     httpRequest. addHeader ( new HTTPHeader ( "redirect_uri", redirectURL ) ) ;
                     httpRequest. addHeader ( new HTTPHeader ( "scope", "" ) ) ;
                     httpRequest. addHeader ( new HTTPHeader ( "grant_type", "authorization_code" ) ) ;
                 httpResponse = fetchService. fetch (httpRequest ) ;
                  InputStream in =getContentAsStream (httpResponse ) ;
                      BufferedReader reader = new BufferedReader ( new InputStreamReader (in ) ) ;
                      String line ;
                      String output = "" ;
                      while ( (line = reader. readLine ( ) ) != null ) {
                              output =output +line ;
              }
              reader. close ( ) ;
              return output ;
                      } catch ( Exception e ) {
                              e. printStackTrace ( ) ;
                              System. out. println (e. getMessage ( ) ) ;
                              return e. getMessage ( ) ;
                      }
                     
            }
            public static byte [ ] getContentAsBytes (HTTPResponse httpResponse ) {
                  return httpResponse. getContent ( ) ;
                }
     
                public static InputStream  getContentAsStream (HTTPResponse httpResponse ) {
                  return new ByteArrayInputStream (getContentAsBytes (httpResponse ) ) ;
                }
    }
package com.login.client;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import com.google.appengine.api.urlfetch.HTTPHeader;
import com.google.appengine.api.urlfetch.HTTPMethod;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;

public class FetchUrl {
	private static String client_id = "<Client ID>";
	private static String client_secret = "<client_secret>";
	private static String redirectURL = "http://gfksgnbgfcv.appspot.com/index2.jsp";
	private static URLFetchService fetchService;
	public static String getParseValue(String code){
		HTTPResponse httpResponse=null;
		  try {
		 String urlStr="https://accounts.google.com/o/oauth2/token";
		 URL url = new URL(urlStr);
		 HTTPMethod method=HTTPMethod.POST;
		 HTTPRequest httpRequest = new HTTPRequest(url,method);
		 fetchService = URLFetchServiceFactory.getURLFetchService();
		 httpRequest.setHeader(new HTTPHeader("Content-Type", "application/x-www-form-urlencoded"));
		 httpRequest.setHeader(new HTTPHeader("Content-length", "250"));
		 httpRequest.addHeader(new HTTPHeader("code", code));
		 httpRequest.addHeader(new HTTPHeader("client_id", client_id));
		 httpRequest.addHeader(new HTTPHeader("client_secret", client_secret));
		 httpRequest.addHeader(new HTTPHeader("redirect_uri", redirectURL));
		 httpRequest.addHeader(new HTTPHeader("scope", ""));
		 httpRequest.addHeader(new HTTPHeader("grant_type", "authorization_code"));
	     httpResponse = fetchService.fetch(httpRequest);
	     InputStream in=getContentAsStream(httpResponse);
		  BufferedReader reader = new BufferedReader( new InputStreamReader(in));
		  String line;
		  String output="";
		  while ((line = reader.readLine()) != null) {
			  output=output+line;
          }
          reader.close();
          return output;
		  }catch(Exception e){
			  e.printStackTrace();
			  System.out.println(e.getMessage());
			  return e.getMessage();
		  }
		  
	}
	public static byte[] getContentAsBytes(HTTPResponse httpResponse) {
	      return httpResponse.getContent();
	    }

	    public static InputStream  getContentAsStream(HTTPResponse httpResponse) {
	      return new ByteArrayInputStream(getContentAsBytes(httpResponse));
	    }
}


你可能感兴趣的:(How to fetch access token in Google AppEngine (OAuth 2.0) us)