2014-05-22 2 views

답변

1

At this repo, is an example 라이브러리 사용 방법. 다음은 주요 샘플의 코드입니다 :

package com.google.api.services.samples.dailymotion.cmdline; 

import com.google.api.client.auth.oauth2.AuthorizationCodeFlow; 
import com.google.api.client.auth.oauth2.BearerToken; 
import com.google.api.client.auth.oauth2.ClientParametersAuthentication; 
import com.google.api.client.auth.oauth2.Credential; 
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; 
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; 
import com.google.api.client.http.GenericUrl; 
import com.google.api.client.http.HttpRequest; 
import com.google.api.client.http.HttpRequestFactory; 
import com.google.api.client.http.HttpRequestInitializer; 
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.JsonObjectParser; 
import com.google.api.client.json.jackson2.JacksonFactory; 
import com.google.api.client.util.store.DataStoreFactory; 
import com.google.api.client.util.store.FileDataStoreFactory; 

import java.io.IOException; 
import java.util.Arrays; 


/** 
* A sample application that demonstrates how the Google OAuth2 library can be used to authenticate 
* against Google. 
* 
* @author Brad Parks 
*/ 
public class GoogleAuthExample { 

    // ********************************************************************** 
    // CHANGE THE FOLLOWING values to the keys you get after following the steps at the following page: 
    // https://developers.google.com/accounts/docs/OAuth2Login#appsetup 
    // This should be all you need to do to get this sample to work. 
    // ********************************************************************** 
    public static final String API_KEY = "Enter your key here"; 
    public static final String API_SECRET = "Enter your key here"; 

    /** Directory to store user credentials. */ 
    private static final java.io.File DATA_STORE_DIR = 
     new java.io.File(System.getProperty("user.home"), ".store/google_oauth_sample"); 

    /** 
    * Global instance of the {@link DataStoreFactory}. The best practice is to make it a single 
    * globally shared instance across your application. 
    */ 
    private static FileDataStoreFactory DATA_STORE_FACTORY; 

    /** OAuth 2 scope. */ 
    private static final String SCOPE = "openid email profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"; 

    /** Global instance of the HTTP transport. */ 
    private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport(); 

    /** Global instance of the JSON factory. */ 
    static final JsonFactory JSON_FACTORY = new JacksonFactory(); 

    private static final String TOKEN_SERVER_URL = "https://accounts.google.com/o/oauth2/token"; 
    private static final String AUTHORIZATION_SERVER_URL = "https://accounts.google.com/o/oauth2/auth"; 
    public static final int PORT = 8080; 
    public static final String DOMAIN = "127.0.0.1"; 


    /** Authorizes the installed application to access user's protected data. */ 
    private static Credential authorize() throws Exception { 
    errorIfNotSpecified(); 

    AuthorizationCodeFlow flow = new AuthorizationCodeFlow.Builder(BearerToken 
     .queryParameterAccessMethod(), 
     HTTP_TRANSPORT, 
     JSON_FACTORY, 
     new GenericUrl(TOKEN_SERVER_URL), 
     new ClientParametersAuthentication(
      API_KEY, API_SECRET), 
     API_KEY, 
     AUTHORIZATION_SERVER_URL).setScopes(Arrays.asList(SCOPE)) 
     .setDataStoreFactory(DATA_STORE_FACTORY).build(); 

    // authorize 
    LocalServerReceiver receiver = new LocalServerReceiver.Builder().setHost(DOMAIN).setPort(PORT).build(); 
    return new AuthorizationCodeInstalledApp(flow, receiver).authorize("user"); 
    } 

    public static void errorIfNotSpecified() { 
    if (API_KEY.startsWith("Enter ") || API_SECRET.startsWith("Enter ")) { 
     System.out.println(
      "Enter API Key and API Secret from https://developers.google.com/accounts/docs/OAuth2Login#appsetup" 
      + " into API_KEY and API_SECRET in " + GoogleAuthExample.class); 
     System.exit(1); 
    } 
    } 

    private static void run(HttpRequestFactory requestFactory) throws IOException { 
    GenericUrl url = new GenericUrl("https://www.googleapis.com/oauth2/v1/tokeninfo"); 

    HttpRequest request = requestFactory.buildGetRequest(url); 
    UserInfo userInfo = request.execute().parseAs(UserInfo.class); 

    System.out.println("Got user info from API after authorization:"); 
    System.out.println("-----------------------------------------------"); 

    System.out.println("issued_to: " + userInfo.issued_to); 
    System.out.println("audience: " + userInfo.audience); 
    System.out.println("user_id: " + userInfo.user_id); 
    System.out.println("scope: " + userInfo.scope); 
    System.out.println("expires_in: " + userInfo.expires_in); 
    System.out.println("email: " + userInfo.email); 
    System.out.println("verified_email: " + userInfo.verified_email); 
    System.out.println("access_type: " + userInfo.access_type); 
    } 

    public static void main(String[] args) { 
    try { 
     DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); 
     final Credential credential = authorize(); 
     HttpRequestFactory requestFactory = 
      HTTP_TRANSPORT.createRequestFactory(new HttpRequestInitializer() { 
      @Override 
      public void initialize(HttpRequest request) throws IOException { 
       credential.initialize(request); 
       request.setParser(new JsonObjectParser(JSON_FACTORY)); 
      } 
      }); 
     run(requestFactory); 
     // Success! 
     return; 
    } catch (IOException e) { 
     System.err.println(e.getMessage()); 
    } catch (Throwable t) { 
     t.printStackTrace(); 
    } 
    System.exit(1); 
    } 
} 

UserInfo.java

package com.google.api.services.samples.dailymotion.cmdline; 

import com.google.api.client.util.Key; 

public class UserInfo { 
    @Key 
    public String issued_to; 

    @Key 
    public String audience; 

    @Key 
    public String user_id; 

    @Key 
    public String scope; 

    @Key 
    public Integer expires_in; 

    @Key 
    public String email; 

    @Key 
    public Boolean verified_email; 

    @Key 
    public String access_type; 
} 
+0

이 샘플에서'https : // www.googleapis.com/oauth2/v1/tokeninfo' 호출은 Google에만 해당되며 추가 HTTPS 호출을 추가합니다. 여러 공급자와 OpenID Connect를 사용하는 상호 운용 방식은 표준 형식이므로 각 IdP에서 ID 토큰을 디코딩하는 것입니다. –

0

당신은 oxProx 사용을 고려할 수 있습니다, 그리고 오픈 ID 프록시를 연결합니다.

프록시가 새로운 id_token을 발행했기 때문에 프록시 뒤의 클라이언트를 발견하고 활성화 할 수있는 몇 가지 문제점이 해결되었습니다. 즉, 올바른 aud가 각각의 고객).