如何利用Google OAuth2 for Java lib 拿到access token,并用其访问我的网络资源
各位好,
我是一个JAVA新人,最近需要用google 的OAuth2 的java 库做一些事情,不知道如何下手,请指教。
程序要求如下:
用java程序实现一个安全认证,利用google OAuth2 拿到access token并访问受保护资源。
我在网上找到一些资源,写了如下代码。说实话,我不是特别理解这段代码。
首先跳转url如何定?我是打算在web server端实现这段code的,调试时是http:\\localhost,访问的受保护资源是https:\\staging.xxxx.com\v2\application。
其次,如何构造手访问资源的request?网上有一些访问google app的例子,但是不太明白,不知道怎么用。适合我这种要求吗。可否贡献一个这样的例子。
谢谢
我是一个JAVA新人,最近需要用google 的OAuth2 的java 库做一些事情,不知道如何下手,请指教。
程序要求如下:
用java程序实现一个安全认证,利用google OAuth2 拿到access token并访问受保护资源。
我在网上找到一些资源,写了如下代码。说实话,我不是特别理解这段代码。
首先跳转url如何定?我是打算在web server端实现这段code的,调试时是http:\\localhost,访问的受保护资源是https:\\staging.xxxx.com\v2\application。
其次,如何构造手访问资源的request?网上有一些访问google app的例子,但是不太明白,不知道怎么用。适合我这种要求吗。可否贡献一个这样的例子。
谢谢
private static final Collection<String> SCOPE = Arrays.asList("https://www.googleapis.com/auth/urlshortener");
private static final String CALLBACK_URL = "urn:ietf:wg:oauth:2.0:oob";
private static final HttpTransport TRANSPORT = new NetHttpTransport();
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
private static final String CLIENT_ID = "xxxxx"; //use your client ID
private static final String CLIENT_SECRET = "xxxxx"; //use your client secret
GoogleAuthorizationCodeFlow authorizationCodeFlow = new GoogleAuthorizationCodeFlow
.Builder(TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET, SCOPE)
.setCredentialDataStore(new MemoryDataStoreFactory().getDataStore("tokens"))
.build();
String userId = "user-id";
Credential credential = authorizationCodeFlow.loadCredential(userId);
if (credential == null) {
GoogleAuthorizationCodeRequestUrl authorizationUrl = authorizationCodeFlow.newAuthorizationUrl();
authorizationUrl.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI);
//LOGGER.error("Please, authorize application. Visit {}", authorizationUrl);
Scanner s = new Scanner(System.in);
String code = s.nextLine();
GoogleAuthorizationCodeTokenRequest tokenRequest = authorizationCodeFlow.newTokenRequest(code);
tokenRequest.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI);
GoogleTokenResponse tokenResponse = tokenRequest.execute();
credential = authorizationCodeFlow.createAndStoreCredential(tokenResponse, userId);
...全文
相关推荐
最新版本ArcGIS_For_Android_Eclipse环境配置
最新版本ArcGIS_For_Android_Eclipse环境配置
在android中使用KSOAP2调用webservice
简述在android中使用ksoap2调用webservice的方法
Android中ksoap2-android调用WebService 实现天气预报
Android中ksoap2-android调用WebService 实现天气预报,4.0版本好像不能运行,但是可以参考学习。
gwt-spring
springone springgwt_sudoku demo 博文链接:https://eric2007.iteye.com/blog/209563
刚做的2款应用 FlyAndTrain.apk CompareProduct.apk
NULL 博文链接:https://fengzhizi715.iteye.com/blog/686473


Web 开发

