I am developing an android application that has the potential to provide large amount of statistical information. I want to save this data on my google drive to be analyzed later.
However, I am new to Google apis and have little to no idea how to authorize my account programatically. Here is what I have so far.
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);
}
}
The code runs sucessfully and the file is created on the device but I want to send it to my google drive.
I think the main problem is that I don't know what the account name credential is supposed to be. I have a feeling that it isn't "myacc@gmail.com". Is there any way I can obtain the account name string for my own google account?