本文整理汇总了Java中com.google.api.client.auth.oauth2.Credential.getAccessToken方法的典型用法代码示例。如果您正苦于以下问题:Java Credential.getAccessToken方法的具体用法?Java Credential.getAccessToken怎么用?Java Credential.getAccessToken使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.api.client.auth.oauth2.Credential
的用法示例。
在下文中一共展示了Credential.getAccessToken方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doInBackground
import com.google.api.client.auth.oauth2.Credential; //导入方法依赖的package包/类
@Override
protected Boolean doInBackground(Void... params) {
//final String token = SipgateApi.getToken(mEmail, mPassword);
final OAuthManager.OAuthFuture<Credential> credentialOAuthFuture = oauth.authorizeExplicitly(email);
String token = null;
try {
final Credential result = credentialOAuthFuture.getResult();
token = result.getAccessToken();
}
catch (IOException e) {
e.printStackTrace();
}
if (token == null) { return false; }
AccountManager accountManager = AccountManager.get(SipgateLoginActivity.this); //this is Activity
Account account = new Account(email, getString(R.string.account_type));
final Bundle extras = new Bundle();
extras.putString("token", token);
boolean success = accountManager.addAccountExplicitly(account, null, extras);
if (success) {
accountManager.setAuthToken(account, "oauth", token);
Log.d(TAG, "Account created");
}
else {
Log.d(TAG, "Account creation failed. Look at previous logs to investigate");
}
ContentResolver.requestSync(account, ContactsContract.AUTHORITY, extras);
ContentResolver.setSyncAutomatically(account, ContactsContract.AUTHORITY, true);
ContentResolver.addPeriodicSync(account, ContactsContract.AUTHORITY, extras, 60 * 60 * 24);
ContentResolver.setIsSyncable(account, ContactsContract.AUTHORITY, 1);
return true;
}
示例2: onSuccess
import com.google.api.client.auth.oauth2.Credential; //导入方法依赖的package包/类
@Override
protected void onSuccess(HttpServletRequest req, HttpServletResponse resp, Credential credential) throws IOException {
//https://coderanch.com/t/542459/java/GoogleAPI-user-info
//https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=%ACCESS_TOKEN%
HttpClient httpClient = new DefaultHttpClient();
HttpUriRequest request = new HttpGet("https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token="+credential.getAccessToken());
HttpResponse res = httpClient.execute(request);
JsonParser parser = JacksonFactory.getDefaultInstance().createJsonParser(res.getEntity().getContent());
parser.nextToken();
UserInfo u = new UserInfo(parser);
parser.close();
Usuario usuario = usuarioFacade.find(u.id);
boolean newcomer = false;
if(usuario == null) {
newcomer = true;
usuario = new Usuario();
usuario.setId(u.id);
usuario.setNombre(u.givenName);
usuario.setApellidos(u.familyName);
usuario.setEmail(u.email);
usuario.setTipo((short) 1); //By default, will be registering with type 1 (normal user)
usuario.setImagen(u.picture);
usuarioFacade.create(usuario);
} else {
//If the user has changed something, reflect that into the DB
usuario.setNombre(u.givenName);
usuario.setApellidos(u.familyName);
usuario.setEmail(u.email);
usuario.setImagen(u.picture);
usuarioFacade.edit(usuario);
}
String token = TokensUtils.createJwtTokenForUserId(u.id);
String callbackUrl = (String) req.getSession().getAttribute("callbackUrl");
if(callbackUrl != null) {
//If we stored the callback URL during the process, return it
resp.sendRedirect(callbackUrl + "?token=" + token + "&newcomer=" + newcomer);
req.getSession().removeAttribute("callbackUrl");
} else {
//Otherwise, return a json with the same info
resp.setContentType("application/json");
resp.setHeader("access-control-allow-origin", "*");
resp.getOutputStream().print("{\"token\": \"" + token + "\", \"newcomer\": " + newcomer + "}"); //Token
resp.getOutputStream().close();
}
}