fc2ブログ

2011-10-19(Wed)

Google+ APIを使ってOAuth2認証しprofileデータをとってみる(3)

Controllerですべてできたー説明はコメントに記載する。


public class GoauthController extends Controller{
private final static Logger LOG = Logger.getLogger(GoauthController.class.getName());
private final static String CLIENT_ID = "*********************************************";
private final static String CLIENT_SECRET = "***************";
private final static String REDIRECT_URL = "http://***************/Goauth/callback";
private final static String GOOGLE_API_KEY = "***************";
private final static String OAUTH_SCOPES = "https://www.googleapis.com/auth/plus.me";

/** Getですべてをまかなう */
public Navigation doGet(){

response.setContentType("application/json");

String error = param("error");
/** Callbackのパラメータにerrorが含まれていればParmission Errorにする */
if (error != null) {
response.setContentType("text/plain");
try {
response.getWriter().println("There was a problem during authentication: " + error);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
LOG.severe("There was a problem during authentication: " + error);
return null;
}

String code = param("code");
/** Callbackのパラメータにcodeが含まれていればOAuth2認証成功 */
if (code == null || code.isEmpty()) {
/** 認証スタートするところ(パラメータにcodeが含まれていない) */
// Now that we have the OAuth 2.0 code, we must exchange it for a token to make API requests.
// Build the authorization URL
AuthorizationRequestUrl authorizeUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID, REDIRECT_URL, OAUTH_SCOPES);

authorizeUrl.redirectUri = REDIRECT_URL;
authorizeUrl.scope = OAUTH_SCOPES;
String authorizationUrl = authorizeUrl.build();

LOG.info("Redirecting browser for OAuth 2.0 authorization to " + authorizationUrl);
try {
   /** 認証スタート */
response.sendRedirect(authorizationUrl);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}else{
/** Callbackがくるところ */
LOG.info("Exchanging OAuth code for access token using server side call");

LOG.info("callback start");
/** AccessTokenRequestを取得 */
AccessTokenResponse accessTokenResponse = null;
try {
accessTokenResponse = new GoogleAccessTokenRequest.GoogleAuthorizationCodeGrant(
new NetHttpTransport(),
new GsonFactory(),
CLIENT_ID,
CLIENT_SECRET,
code,
REDIRECT_URL
).execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

/** AccessTokenがとれたーー */
LOG.info("Storing authentication token into the session");
LOG.info("accessToken: " + accessTokenResponse.accessToken);
LOG.info("refreshToken: " + accessTokenResponse.refreshToken);

Plus unauthenticatedPlus = new Plus(Util.TRANSPORT, Util.JSON_FACTORY);
unauthenticatedPlus.setKey(GOOGLE_API_KEY);
GoogleAccessProtectedResource requestInitializer =
new GoogleAccessProtectedResource(
accessTokenResponse.accessToken,
Util.TRANSPORT,
Util.JSON_FACTORY,
CLIENT_ID,
CLIENT_SECRET,
accessTokenResponse.refreshToken
);
Plus plus = new Plus(Util.TRANSPORT, requestInitializer, Util.JSON_FACTORY);
Person profile = null;

try {
profile = plus.people.get("me").execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return redirect("/");
}
}
return null;
}



関連記事
スポンサーサイト



コメントの投稿

管理者にだけ表示を許可する

コメント

プロフィール

kumagonjp2

Author:kumagonjp2
Python,Django,R,Mongo,MySQL,Struts,Spring,データマイニングなどサーバー関係のメモを残していきます。

最新記事
最新コメント
最新トラックバック
月別アーカイブ
カテゴリ
雪が3Dで降るブログパーツ ver2

マウスで見る方向変えられます

検索フォーム
RSSリンクの表示
リンク
ブロとも申請フォーム

この人とブロともになる

QRコード
QR