Search in sources :

Example 1 with OAuthService

use of org.eclipse.egit.github.core.service.OAuthService in project hubroid by EddieRingle.

the class GitHubAccountAuthenticator method getAuthToken.

@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle options) throws NetworkErrorException {
    String password = AccountManager.get(mContext).getPassword(account);
    Authorization auth = null;
    try {
        GitHubClient client = new GitHubClient();
        client.setUserAgent(USER_AGENT_STRING);
        client.setCredentials(account.name, password);
        OAuthService service = new OAuthService(client);
        for (Authorization a : service.getAuthorizations()) {
            if (a != null && a.getNote() != null) {
                if (a.getNote().equals(DESCRIPTION_CLIENT)) {
                    auth = a;
                }
            }
        }
        if (auth == null) {
            auth = new Authorization();
            auth.setNote(DESCRIPTION_CLIENT);
            auth.setNoteUrl(CLIENT_URL);
            List<String> scopes = new ArrayList<String>();
            scopes.add("user");
            scopes.add("repo");
            scopes.add("gist");
            auth.setScopes(scopes);
            auth = service.createAuthorization(auth);
        }
    } catch (IOException e) {
        throw new NetworkErrorException(e);
    }
    String oauthToken = auth.getToken();
    Bundle bundle = new Bundle();
    bundle.putString(KEY_ACCOUNT_NAME, account.name);
    bundle.putString(KEY_ACCOUNT_TYPE, GITHUB_ACCOUNT_TYPE);
    bundle.putString(KEY_AUTHTOKEN, oauthToken);
    return bundle;
}
Also used : Authorization(org.eclipse.egit.github.core.Authorization) NetworkErrorException(android.accounts.NetworkErrorException) GitHubClient(org.eclipse.egit.github.core.client.GitHubClient) OAuthService(org.eclipse.egit.github.core.service.OAuthService) Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Aggregations

NetworkErrorException (android.accounts.NetworkErrorException)1 Bundle (android.os.Bundle)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Authorization (org.eclipse.egit.github.core.Authorization)1 GitHubClient (org.eclipse.egit.github.core.client.GitHubClient)1 OAuthService (org.eclipse.egit.github.core.service.OAuthService)1