I am developing an Android application that can provide a large amount of statistical information. I want to save this data on my Google drive for later analysis.
However, I am new to Google apis and know little how to authorize my account programmatically. Here is what I still have.
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 successfully 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 how the account name should be indicated. I have a feeling that this is not " myacc@gmail.com ". Is there a way to get the account name string for my google account?
source share