我找不到上传到Google云端硬盘的文件

时间:2019-06-23 18:05:03

标签: android android-studio google-drive-api google-drive-android-api

我正在尝试使用Google Drive将文件上传到Google Drive REST API v3。上载过程完成后,它返回status code of 200(成功)。但是我在Google Drive中找不到文件。请告诉我我做错了什么?如果您在为我解决此问题的同时提供了正确的插图或更好的代码段,将不胜感激。我真的很期待您的回答。 我尝试按照文档进行操作,但仍然遇到相同的错误。我已经到处搜索了在线和stackoverflow,但是似乎没有一个可以解决我的问题。 这是代码

 private val AUTHORIZATION_PARAM = "Authorization"
    private val BEARER_VAL = "Bearer "
    private val CONTENT_TYPE_PARAM = "Content-Type: "
    private val LINE_FEED = "\r\n"
    private val APP_FOLDER_ID = "appDataFolder"



    fun connectAndStartOperation() {
        if (mAuthCode == null) {
            signInOptions = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestEmail()
                .requestProfile()
                .requestScopes(Scope(Scopes.DRIVE_APPFOLDER))
                .requestIdToken(resources.getString(R.string.gdrive_clientId))
                .requestServerAuthCode(resources.getString(R.string.gdrive_clientId))
                .build()

            mGoogleSignInClient = GoogleSignIn.getClient(this, signInOptions!!)


            startActivityForResult(mGoogleSignInClient?.signInIntent, CLOUD_STORAGE)


            Log.i("mAuthCode", "false")
        } else {
            Log.i("mAuthCode", "true")
            writeDbToDrive()
            mNextGoogleApiOperation = INVALID;
        }
    }


    fun disconnect() {
        mGoogleSignInClient?.signOut()
        mActivity = null
        mNextGoogleApiOperation = INVALID
        mAuthCode = null
        mAccessToken = null
        mTokenExpired = 0
    }

    override fun onDestroy() {
        disconnect()
        super.onDestroy()

    }

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode == CLOUD_STORAGE) {
            val task = GoogleSignIn.getSignedInAccountFromIntent(data)
                .addOnSuccessListener(this)
                .addOnFailureListener(this)


        }
    }

    override fun onSuccess(googleSignInAccount: GoogleSignInAccount?) {


        Log.i("mAuthCode", "Success")
        val scope = "oauth2:https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.profile"

        idTokenString = googleSignInAccount?.idToken
        mAuthCode = googleSignInAccount?.serverAuthCode
        mGoogleSignInAccount = googleSignInAccount

        doAsync {
            try {
                mAccessToken = GoogleAuthUtil.getToken(this@SettingsActivity, mGoogleSignInAccount?.account, scope)
            } catch (e: Exception) {
                Log.i("Error AccessToken", "${e.message}")

                e.printStackTrace()
            }

            uiThread {
                Log.i("AccessTokenMy", "$mAccessToken")
            }
        }


    }


    override fun onFailure(p0: java.lang.Exception) {
        Log.i("mAuthCode", "Failed")

        p0.printStackTrace()
    }


    private fun writeDbToDrive() {
        var conn: HttpURLConnection? = null
        var os: OutputStream? = null


        val accessToken = requestAccessToken(mGoogleSignInAccount!!)

        if (accessToken == null)
            return



        try {
            val boundary = "pb" + System.currentTimeMillis()
            val url = URL("https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart")
            conn = url.openConnection() as HttpURLConnection
            conn.requestMethod = "POST"
            conn.useCaches = false
            conn.doOutput = true
            conn.doInput = true
            conn.connectTimeout = 5000
            conn.setRequestProperty(AUTHORIZATION_PARAM, BEARER_VAL + accessToken!!)
            conn.setRequestProperty("Content-Type", "multipart/related; boundary=$boundary")

            Log.i("Action", "Parameter set for server")

            /////// Prepare data
            //val timestamp = SimpleDateFormat("yyyy-MM-dd_HH:mm:ss", Locale.getDefault()).format(Date())
            // Prepare file metadata (Change your backup file name here)
            val b = StringBuilder()
            b.append('{')
                .append("\"name\":\"").append(exportedFileName).append('\"')
                .append(',')
                .append("\"mimeType\":").append("\"text\\/csv\"")
                .append(',') //"\"application\\/vnd.google-apps.unknown\""
                .append("\"parents\":").append("[\"").append(APP_FOLDER_ID).append("\"]")
                .append('}')
            val metadata = b.toString()
            val data = readFile(File(filePath))

            /////// Calculate body length
            var bodyLength = 0
            // MetaData part
            b.setLength(0)
            b.append("--").append(boundary).append(LINE_FEED)
            b.append(CONTENT_TYPE_PARAM).append("application/json; charset=UTF-8")
                .append(LINE_FEED)
            b.append(LINE_FEED)
            b.append(metadata).append(LINE_FEED)
            b.append(LINE_FEED)
            b.append("--").append(boundary).append(LINE_FEED)
            b.append(CONTENT_TYPE_PARAM).append("text/csv").append(LINE_FEED)
            b.append(LINE_FEED)
            val beforeFilePart = b.toString().toByteArray(charset("UTF_8"))
            bodyLength += beforeFilePart.size

            bodyLength += data.size // File

            b.setLength(0)
            b.append(LINE_FEED)
            b.append("--").append(boundary).append("--")
            val afterFilePart = b.toString().toByteArray(charset("UTF_8"))
            bodyLength += afterFilePart.size

            conn.setRequestProperty("Content-Length", bodyLength.toString())
            //if (BuildConfig.DEBUG_MODE) DebugHelper.log("LENGTH", bodyLength)

            /////// Write to socket
            os = conn.outputStream

            try {
                os!!.write(beforeFilePart)
                os!!.write(data)
                os!!.write(afterFilePart)
                os!!.flush()
            } catch (e: Exception) {
                e.printStackTrace()
            }


            val msg = conn.responseMessage
            val code = conn.responseCode


            if (code == 200) {
                Log.i("writeDbToDrive", "Exported Successfully: $code $msg")

            } else {
                Log.i("writeDbToDrive", "Error: $code $msg")

            }
        } catch (e: Exception) {
            e.printStackTrace()
            Log.i("writeDbToDrive", e.message!!)
        } finally {
            if (os != null) {
                try {
                    os!!.close()
                } catch (e: IOException) {
                }

            }
            conn?.disconnect()
        }
    }

    @Throws(IOException::class)
    private fun readFile(file: File): ByteArray {
        val f = RandomAccessFile(file, "r")
        try {
            val longlength = f.length()
            val length = longlength.toInt()
            if (length.toLong() != longlength)
                throw IOException("File size >= 10 Mb")

            val data = ByteArray(length)
            f.readFully(data)
            return data
        } finally {
            f.close()
        }
    }

    private fun requestAccessToken(mGoogleSignInAccount: GoogleSignInAccount): String? {

        val scope = "oauth2:https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.profile"



        doAsync {
            try {
                mAccessToken = GoogleAuthUtil.getToken(this@SettingsActivity, mGoogleSignInAccount?.account, scope)
            } catch (e: Exception) {
                Log.i("Error AccessToken", "${e.message}")

                e.printStackTrace()
            }

            uiThread {
                Log.i("AccessTokenMy", "$mAccessToken")
            }

        }

        return mAccessToken
    }

1 个答案:

答案 0 :(得分:0)

在阅读完此Files: create文档之后,我终于解决了该问题。我不知道文件保存在我的应用创建的AppData文件夹中。 AppData文件夹被隐藏,只能由我的应用程序访问或通过我的应用程序访问。为了能够将文件保存到My Drive文件夹中,我删除了部分元数据

          ` .append(',') 
            .append("\"parents\":").append("[\"").append(APP_FOLDER_ID).append("\"]")`

所以元数据部分现在是这样的

 val b = StringBuilder()
        b.append('{')
            .append("\"name\":\"").append(exportedFileName).append('\"')
            .append(',')
            .append("\"mimeType\":").append("\"text\\/csv\"")
            .append('}')
        val metadata = b.toString()

其他所有东西都保持不变