我正在开发一个有可能提供大量统计信息的 android 应用程序。我想将这些数据保存在我的谷歌驱动器上以供以后分析。
但是,我是 Google api 的新手,几乎不知道如何以编程方式授权我的帐户。这是我到目前为止所拥有的。
private static Uri driveUri;
private static Drive service;
private GoogleAccountCredential credential;
private Account driveAccount;
// AccountManager accManager =
// (AccountManager) getSystemService(Context.ACCOUNT_SERVICE);
// accManager.setUserData(driveAccount, ???, ???)
credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
credential.setSelectedAccountName("myaccount@gmail.com");
service = getDriveService(credential);
public class UploadToDrive extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US).format(new Date());
File sdcard = new File(Environment.getExternalStorageDirectory().getPath());
File fileAnalytics = null;
try {
fileAnalytics = File.createTempFile("Sessions" + File.separator + "session_" + timeStamp, ".txt", sdcard);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
PrintStream ps = null;
try {
ps = new PrintStream(fileAnalytics);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ps.println("Count: " + count);
ps.println("Time: " + (System.currentTimeMillis() - startTime));
ps.close();
FileContent content = new FileContent("text/plain", fileAnalytics);
com.google.api.services.drive.model.File body = new com.google.api.services.drive.model.File();
body.setTitle(fileAnalytics.getName());
body.setMimeType("text/plain");
try {
service.files().insert(body, content);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
}
代码成功运行,文件在设备上创建,但我想将它发送到我的谷歌驱动器。
我认为主要问题是我不知道帐户名称凭据应该是什么。我有一种感觉,它不是“myacc@gmail.com”。有什么方法可以获取我自己的 google 帐户的帐户名称字符串?