當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。