当前位置: 首页>>代码示例>>Java>>正文


Java Credential.getAccessToken方法代码示例

本文整理汇总了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;
}
 
开发者ID:derveloper,项目名称:android-sipgateSync,代码行数:39,代码来源:SipgateLoginActivity.java

示例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();
    }
}
 
开发者ID:melchor629,项目名称:agendamlgr,代码行数:49,代码来源:OAuthCallbackServlet.java


注:本文中的com.google.api.client.auth.oauth2.Credential.getAccessToken方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。