getting a "Resource not found for the segment " for the file when uploading a file to sharepoint site using graph API

I need to upload a file to a list of a custom site in SharePoint. I have used graph Rest API according to the following URL.

https://docs.microsoft.com/en-us/graph/api/driveitem-put-content?view=graph-rest-1.0&tabs=http

But I'm getting following error when I'm uploading the file.

HTTP response:  {
  "error": {
    "code": "BadRequest",
    "message": "Resource not found for the segment 'FileB.txt'.",
    "innerError": {
      "request-id": "30ccddb7-17b2-4b60-9943-39ce65eb301f",
      "date": "2020-05-18T13:57:36"
    }
  }
}

Это мой пример кода Java.

public void uploadFilesToList() throws MalformedURLException, IOException{

    String tenantId = "48e2d3d3-5602-4db8-b9e4-xxxxxxxxxxxx";
    String clientId = "f08b652a-6e3c-448f-b98c-xxxxxxxxxxxx";
    String clientSecret = "KHII03L.77f6IP2zCm~v~hUXD.a.xxxxx";
    clientSecret=java.net.URLEncoder.encode(clientSecret,"UTF-8");
    String access_token = getaccetAuth(tenantId, clientId, clientSecret);

    String siteId = "xxxxxxxx.sharepoint.com,ce2ae416-288b-41d4-8d58-af491571867a,976efac4-37b3-4515-858a-cdfddc936a39";
    String listId = "8c47c9c5-88cb-4ad9-a94a-57b6dceec2a4";
    String formattedUrl = String.format("https://graph.microsoft.com/v1.0/sites/%s/lists/%s/%s/content",
            siteId,listId,"FileB.txt");

    HttpPut put = null;
    try {

        File file = new File(("C:\\Users\\user_name\\Desktop"+"\\FileB.txt"));
        MultipartEntity entity = new MultipartEntity();
        entity.addPart("file", new FileBody(file));

        HttpClient client = HttpClientBuilder.create().build();
        put = new HttpPut(formattedUrl);
        // add header
        put.setHeader("Content-Type", "text/plain");
        put.setHeader("Authorization", "Bearer " + access_token);
        put.setEntity(entity);

        HttpResponse response = client.execute(put);
        System.out.println("Response Code : "
                + response.getStatusLine().getStatusCode());
        System.out.println("HTTP response:  "+response.toString());
        String json = EntityUtils.toString(response.getEntity());
        System.out.println("HTTP response:  "+json);

    } finally {
        put.releaseConnection();
    }
}

это мой метод получения auth_token.

 public String getaccetAuth(String tnID, String clId, String cliSec) throws IOException {
    String endpoint = String.format("https://login.microsoftonline.com/%s/oauth2/token",tnID);
    String postBody = String.format("grant_type=password&client_id=%s&client_secret=%s&resource=%s&userName=%s&passWord=%s&scope=%s",
            clId, cliSec, "https://graph.microsoft.com","admin@xxxxxx.onmicrosoft.com","xxxxxxx","https://graph.microsoft.com/.default");
    HttpsURLConnection conn = (HttpsURLConnection) new URL(endpoint).openConnection();
    conn.setRequestMethod("POST");
    conn.addRequestProperty("Content_Type","application/x-www-form-urlencoded");
    conn.setDoOutput(true);
    conn.getOutputStream().write(postBody.getBytes());
    conn.connect();
    JsonFactory factory = new JsonFactory();
    JsonParser parser = factory.createParser(conn.getInputStream());
    String accessToken = null;
    while (parser.nextToken() != JsonToken.END_OBJECT) {
        String name = parser.getCurrentName();
        if ("access_token".equals(name)) {
            parser.nextToken();
            accessToken = parser.getText();
        }
    }
    return accessToken;
}

Я не уверен, что здесь пошло не так. Может ли кто-нибудь найти способ сделать это правильно? Примечание: здесь я использовал жестко запрограммированные значения.

1 ответ

Судя по документу, такого api не нашел

https://graph.microsoft.com/v1.0/sites/%s/lists/%s/%s/content

Я пробовал с

https://graph.microsoft.com/v1.0/sites/%s/drive/items/%s:/FileB.txt:/content

и он работал нормально.

Другие вопросы по тегам