2
votes

Hi I’m working on project, which has to access Google Docs. As example I’ve started from here Currently I’m getting “401 Cannot parse AuthSub token” I’ve looked into:

https://developers.google.com/google-apps/documents-list/

http://code.google.com/p/google-api-java-client/

Using Android AccountManager to get authtoken for gdata

And as I understand, android returns wrong type of access token. If there are any way to create valid access token for android using AccountManager, or I need to uses some other way of authentication?

import com.google.api.client.googleapis.extensions.android.accounts.GoogleAccountManager;
import com.google.api.services.docs.DocsClient;
import com.google.api.services.docs.DocsUrl;
import com.google.api.services.docs.model.DocumentListEntry;
import com.google.api.services.docs.model.DocumentListFeed;

public class Main extends ListActivity{
    private static final String AUTH_TOKEN_TYPE = "writely";
    private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    protected DocsClient client;
    String accountName;


    GoogleAccountManager accountManager;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        client = new DocsClient(HTTP_TRANSPORT.createRequestFactory(credential));
        accountManager = new GoogleAccountManager(this);
        gotAccount();
    }

    void gotAccount() {

        accountManager.getAccountManager().getAuthToken(account, AUTH_TOKEN_TYPE, true, new AccountManagerCallback<Bundle>() {

          public void run(AccountManagerFuture<Bundle> future) {
            try {
              Bundle bundle = future.getResult();
              if (bundle.containsKey(AccountManager.KEY_INTENT)) {
                Intent intent = bundle.getParcelable(AccountManager.KEY_INTENT);
                intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivityForResult(intent, REQUEST_AUTHENTICATE);
              } else if (bundle.containsKey(AccountManager.KEY_AUTHTOKEN)) {
                setAuthToken(bundle.getString(AccountManager.KEY_AUTHTOKEN));
                onAuthToken();
              }
            } catch (Exception e) {
              Log.e(TAG, e.getMessage(), e);
            }
          }
        }, null);
  }

void setAuthToken(String authToken) {
    credential.setAccessToken(authToken);
  }

 void onAuthToken() {
    List<String> result = new ArrayList<String>();
    DocumentListFeed feed = client.executeGetDocumentListFeed(DocsUrl.forDefaultPrivateFull());
    for (DocumentListEntry doc : feed.docs) {
            result.add(doc.title);
          }
    //DO something with list
  }
}
1
Please show us the code you are actually using (with credentials stripped of course), not only the tutorial you followed and then point to the exact line where you get the error. - Jan Gerlinger
I've edited my post to include code there. - user1660016

1 Answers

0
votes

I've succeed in implementing this task. Had to switch to version 1.7 of google api client and uses

com.google.api.client.googleapis.auth.clientlogin.ClientLogin.Response

instead of

com.google.api.client.googleapis.auth.oauth2.GoogleCredential