Linked Questions

Popular Questions

Onedrive api to upload file

Asked by At

I went through many references(including onedrive api doc) but i'm unable to upload a file into onedrive using okhttp request.

The following is the code that I tried:

    try {

            boolean status;

            String url = String.format("onedrive.live.com/v1.0/drive/root:/"+step.file_name+":/content"); 
//exceptions are not coming if i mention https:// in the url. but no output is coming

            Request request = new Request.Builder()
                    .url(url)
                    .put(RequestBody.create(MediaType.parse(step.getContent_type()),step.getFile_data()))
                    .addHeader("Authorization", String.format("Bearer %s",step.getAccess_token()))
                    .build();

            OkHttpClient okHttpClient = new OkHttpClient();


                Response response = okHttpClient.newCall(request).execute();

                status = response.code()==200 ? true : false;

            } catch (Exception e) {
                e.printStackTrace();
                return StepStatus.FAILURE;
            }

When trying from postman (PUT mapping) with the url "onedrive.live.com/v1.0/drive/root:/abc.txt:/content", it is redirecting to drive but saying the file may be deleted.

But, when I'm trying to generate url with file name and hit the drive, it's Showing success but the file is not saved/uploaded in drive.

How to upload a file to onedrive from the okhttp request?

Related Questions