nextcloud / server

☁️ Nextcloud server, a safe home for all your data
https://nextcloud.com
GNU Affero General Public License v3.0
26.76k stars 4k forks source link

I can't share file via Guzzle (PHP, Laravel) #22512

Closed opasgruz closed 3 years ago

opasgruz commented 4 years ago

I am try to share existing file on NextCloud server. This is not my server, i have only access to account and API. NextCloud server version 19.0.2. I am using new API without examples https://docs.nextcloud.com/server/stable/developer_manual/client_apis/OCS/ocs-share-api.html#create-a-new-share and old API with examples https://doc.owncloud.com/server/developer_manual/core/apis/ocs-share-api.html#create-a-new-share. After this request i recived statusCode 200 (it must be 100, 400, 403 or 404 on API documentation ) and file is not shared. use GuzzleHttp\Client as Client;

$client = new Client([
        'base_uri' => 'https://cloud.er.ru/ocs/v2.php/apps/files_sharing/api/v1',
    ]);
    try {
        $response = $client->post('/shares', [
            'auth' => [{username}, {password}],
            'debug' => true,
            'form_params' => [
                'path' => '/folder.jpg',
                'shareType' => 3,
                'permissions' => 1
            ],
            'headers' => [
                'OCS-APIRequest' => true,
            ]
        ]);
        dd($response);
        exit;
    } catch (\GuzzleHttp\Exception\ClientException $e) {
        dd($e->getMessage());
    }

Response: GuzzleHttp\Psr7\Response {#1844 ▼ -reasonPhrase: "OK" -statusCode: 200 -headers: array:21 [▶] -headerNames: array:21 [▶] -protocol: "1.1" -stream: GuzzleHttp\Psr7\Stream {#1842 ▶} }

opasgruz commented 4 years ago

I am found the solution. But Guzzle still not working. $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://cloud.er.ru/ocs/v2.php/apps/files_sharing/api/v1/shares');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "path=/folder.jpg&shareType=3&permissions=1");
    curl_setopt($ch, CURLOPT_USERPWD, {username} . ':' . {password});

    $headers = array();
    $headers[] = 'Ocs-Apirequest: true';
    $headers[] = 'Content-Type: application/x-www-form-urlencoded';
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        echo 'Error:' . curl_error($ch);
    }
    curl_close($ch);

dd($result);exit;

szaimen commented 3 years ago

Hi, please ask your question here: https://help.nextcloud.com/c/dev/11 Thanks!