認真細緻地翻着大量的網頁(主要是矛盾)我不能寫一個簡單的Android應用程序,它用的OAuth2連接到谷歌後:目前編寫Google Tasks或Drive App的步驟是什麼?
我開始使用此頁:
https://developers.google.com/google-apps/tasks/oauth-and-tasks-on-android#account
我沒有下載最新的庫,不得不經歷很多過時的或缺少的功能:
- GoogleAccountManager已被棄用
- HttpTransport,AndroidHttp,AccessProtectedResource,GoogleAccessProtectedResource,JacksonFactory不會在當前庫中找到 CLIENT_ID的
- 代通開發者控制檯的工作祕密
- 代移動應用程序沒有工作,但需要
什麼是最新的描述?什麼是最新的演示?
以防萬一 - 這是我迄今爲止所做的。在processTokenReceived方法都不是的庫的一部分:
public class ActivityMain extends Activity implements DialogInterface.OnClickListener {
private static final String AUTH_TOKEN_TYPE = "oauth2:https://www.googleapis.com/auth/drive";
private static final String CLIENT_ID = "xxxxxxxxxxxx.apps.googleusercontent.com";
private AccountManager accountManager;
private Account[] accounts;
private String authName;
private String authToken;
@Override
public void onClick(final DialogInterface dialogInterface, final int item) {
processAccountSelected(accounts[item]);
}
@Override
public void onCreate(final Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activitymain);
accountManager = AccountManager.get(this);
accounts = accountManager.getAccountsByType("com.google");
if (accounts == null || accounts.length == 0) {
// TODO
} else if (accounts.length == 1) {
processAccountSelected(accounts[0]);
} else if (accounts.length > 1) {
showDialog(MyConstants.DIALOG_ACCOUNTCHOSER);
}
}
@Override
protected Dialog onCreateDialog(final int id) {
switch (id) {
case MyConstants.DIALOG_ACCOUNTCHOSER:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
String[] names = new String[accounts.length];
for (int i = 0; i < accounts.length; i++) {
names[i] = accounts[i].name;
}
alertDialogBuilder.setItems(names, this);
alertDialogBuilder.setTitle("Select a Google account");
return alertDialogBuilder.create();
}
return null;
}
private void processAccountSelected(final Account account) {
if (account != null) {
authName = account.name.toString();
if (!Tools.isEmpty(authName)) {
Toast.makeText(this, authName, Toast.LENGTH_LONG).show();
accountManager.getAuthToken(account, AUTH_TOKEN_TYPE, null, this,
new AccountManagerCallback<Bundle>() {
public void run(final AccountManagerFuture<Bundle> future) {
try {
authToken = future.getResult().getString(
AccountManager.KEY_AUTHTOKEN);
processTokenReceived();
} catch (OperationCanceledException exception) {
// TODO
} catch (Exception exception) {
Log.d(this.getClass().getName(), exception.getMessage());
}
}
}, null);
}
}
}
private void processTokenReceived() {
if (!Tools.isEmpty(authToken)) {
HttpTransport transport = AndroidHttp.newCompatibleTransport();
AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(
accessToken);
Tasks service = new Tasks(transport, accessProtectedResource, new JacksonFactory());
service.accessKey = CLIENT_ID;
service.setApplicationName("Google-DriveSample/1.0");
}
}
}
在測試版中移動軟件包?好吧,似乎我最好等到明年;-)無論如何,接受了答案。謝謝你的幫助。 –
Google Play服務現已推出 –