空例外:没有身份验证标头信息 - 甚至设置.setUserToken(auth_token)

时间:2012-03-15 09:10:18

标签: android oauth-2.0 gdata-api accountmanager

我有以下方法:

public String tryGoogleAuthentication(String auth_token){
    ContactsService contactsService = new ContactsService(".....");
    contactsService.setUserToken(auth_token);

    IFeed feed = null;
    try {
        feed = contactsService.getFeed(new URL("https://www.google.com/m8/feeds/  contacts/default/full?max-results=10000"), ContactFeed.class);
    } catch (IOException e) {
        e.printStackTrace();
        return CONST.GOOGLE_AUTH_INVALID_TOKEN;
    } catch (ServiceException e) {          
        e.printStackTrace();
        return CONST.GOOGLE_AUTH_INVALID_TOKEN;
    } catch (NullPointerException e) {
        e.printStackTrace();
        return CONST.GOOGLE_AUTH_INVALID_TOKEN;
    }

    if (feed == null)
        return "";

    String externalId = feed.getId();
    IPerson person = feed.getAuthors().get(0);
    String email = person.getEmail();
    String name = person.getName();
    String nameLang = person.getNameLang();

    System.out.println("externalId: " + externalId);
    System.out.println("email: " + email);
    System.out.println("name: " + name);
    System.out.println("nameLang: " + nameLang);

    return CONST.STATUS_OK;
}

我收到错误:

java.lang.NullPointerException: No authentication header information
at com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96)
at com.google.gdata.util.AuthenticationException.<init>(AuthenticationException.java:67)
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:600)
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:563)
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:552)
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:530)
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:535)
at com.google.gdata.client.Service.getFeed(Service.java:1135)
at com.google.gdata.client.Service.getFeed(Service.java:998)
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:631)
at com.google.gdata.client.Service.getFeed(Service.java:1017)
at ro.servlet.data.ClientAuthenticator.tryGoogleAuthentication(ClientAuthenticator.java:96)
....

请告诉我设置为contactsService的内容(setUserToken除外)才能正常工作?

之前我没有使用过gData(我是一个Android / iPhone开发人员) - 我从android设备中获取了auth字符串(通过让用户确认这一点)并通过安全通道传递给我的服务器 - 现在我想收集一些关于这个联系人的数据(第一个,姓氏和提供者uid - 我需要在我的应用程序中使用用户的数据库)。

我真的需要完成这项任务,所以,如果有人知道如何解决这个问题,请帮助我!

下面的类描述了我从android设备获取auth字符串的方式。

package ro.locationsApp.android.login;

import java.io.IOException;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.HttpParams;
import org.json.JSONObject;
import ro.locationsApp.android.CONST;
import android.R;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AccountList extends ListActivity {
protected AccountManager accountManager;
protected Intent intent;
DefaultHttpClient http_client = getThreadSafeClient();
private Account currentUsedAccount;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    accountManager = AccountManager.get(getApplicationContext());
    Account[] accounts = accountManager.getAccountsByType("com.google");
    String[] names = new String[accounts.length];
    for (int i = 0; i < accounts.length; i++) {
        System.out.println(accounts[i].name);
        names[i] = accounts[i].name;
    }

    this.setListAdapter(new ArrayAdapter<String>(this,
            R.layout.simple_list_item_1, names));
}

@SuppressWarnings("unchecked")
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    Account account = accountManager.getAccountsByType("com.google")[position];     
    accountManager.getAuthToken(account, "ah", false,
            new GetAuthTokenCallback(), null);
    currentUsedAccount = account;
}

public static DefaultHttpClient getThreadSafeClient() {
    DefaultHttpClient client = new DefaultHttpClient();
    ClientConnectionManager mgr = client.getConnectionManager();
    HttpParams params = client.getParams();

    client = new DefaultHttpClient(new ThreadSafeClientConnManager(params, 
            mgr.getSchemeRegistry()), params);


    return client;
}

@SuppressWarnings("rawtypes")
private class GetAuthTokenCallback implements AccountManagerCallback {

    public void run(AccountManagerFuture result) {
        Bundle bundle;
        try {
            bundle = (Bundle) result.getResult();
            Intent intent = (Intent) bundle.get(AccountManager.KEY_INTENT);
            if (intent != null) {
                // User input required
                startActivity(intent);
            } else {
                onGetAuthToken(bundle);                 
            }
        } catch (OperationCanceledException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (AuthenticatorException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
};

protected void onGetAuthToken(Bundle bundle) {
    final String auth_token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
    System.out.println("AUTH TOKEN: " + auth_token);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                JSONObject request = new JSONObject();
                request.put(CONST.ID_ATTR, CONST.ID_GOOGLE_AUTH);
                JSONObject body = new JSONObject();
                body.put(CONST.GOOGLE_AUTH_TOKEN, auth_token);
                request.put(CONST.DATA_ATTR, body);
                JSONObject response = new JSONObject(new RequestHandler().request(DataSource.LOCATIONS_SERVER_URL, request.toString()));
                String bodyResponse = response.optJSONObject(CONST.DATA_ATTR).optString(CONST.STATUS_ATTR);
                if(bodyResponse.equals(CONST.STATUS_OK)){

                }
                else if(bodyResponse.equals(CONST.GOOGLE_AUTH_INVALID_TOKEN)){
                    runOnUiThread(new Runnable() {
                        @SuppressWarnings("unchecked")
                        public void run() {
                            invalidateUserToken(auth_token);
                            accountManager.getAuthToken(currentUsedAccount, "ah", false,
                                    new GetAuthTokenCallback(), null);
                        }
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }).start();
}

public void invalidateUserToken(String token){
    AccountManager accountManager = AccountManager.get(this);
    accountManager.invalidateAuthToken("com.google", token);
}

}

谢谢,

亚历。

1 个答案:

答案 0 :(得分:0)

accountManager.getAuthToken(currentUsedAccount, "ah", false,
                            new GetAuthTokenCallback(), null);

您的accountTokenType错误。联系人API范围为v2http://www.google.com/m8/feeds/v3https://www.google.com/m8/feeds/