嗨我正在開發需要訪問Google文檔的項目。作爲例子,我已經從here 目前我得到「401無法解析AuthSub憑證」開始 我已經研究過:Android:使用AcountManager訪問Google文檔
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
正如我理解,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
}
}
請告訴我們你實際上是使用(與剝離過程的憑證)的代碼,不僅您遵循的教程,然後指向您出錯的確切位置。 –
我編輯了我的帖子,在其中包含代碼。 – user1660016