嗨,我正在开发一个必须访问 Google Docs 的项目。例如,我从这里开始 目前我正在查看“401 无法解析 AuthSub 令牌”:
https://developers.google.com/google-apps/documents-list/
http://code.google.com/p/google-api-java-client/
使用 Android AccountManager 获取 gdata 的 authtoken
据我了解,android 返回错误类型的访问令牌。如果有任何方法可以使用 AccountManager 为 android 创建有效的访问令牌,或者我需要使用其他一些身份验证方式?
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
}
}