nextcloud/android-library

fail to createfolder using bearer access token

ririx2 opened this issue · 5 comments

Help please,

i try access token from Talk-Android and i use that token for startFolderCreation, but the result process is fail.

D/EGL_emulation: eglMakeCurrent: 0xe2c054e0: ver 3 1 (tinfo 0xe2c03710)
D/OwnCloudClient #0: REQUEST MKCOL /remote.php/odav/myfolder
D/OwnCloudClient #0: Location to redirect: https://domaintesting.com/remote.php/odav/myfolder
D/AdvancedSslSocketFactory: Creating SSL Socket with remote domaintesting.com:443, local null:0, params: org.apache.commons.httpclient.params.HttpConnectionParams@fc9745b
... with connection timeout 5000 and socket timeout 30000
I/ServerNameIndicator: SSLSocket implementation: com.android.org.conscrypt.Java8FileDescriptorSocket
I/ServerNameIndicator: SNI done, hostname: domaintesting.com
D/RemoteOperationResult: RemoteOperationResult has processed UNHANDLED_HTTP_CODE: 200 OK
D/CreateFolderRemoteOperation: Create directory myfolder: Operation finished with HTTP status code 200 (fail)

Thanks.

Is there a reason you use bearer access?
Normally we use only basic auth.

Can you also please post a code snippete so we can better discuss this?

I tried adding the file send feature directly from the internal memory in the Talk-Android.
my reference https://docs.nextcloud.com/server/16/developer_manual/android_library/examples.html
my build.gradle implementation 'com.github.nextcloud:android-library:-SNAPSHOT'
i try too
implementation 'com.github.nextcloud:android-library:master-SNAPSHOT'

@AutoInjector(NextcloudTalkApplication.class)
public class ChatController extends BaseController implements MessagesListAdapter.OnLoadMoreListener,
MessagesListAdapter.Formatter<Date>, MessagesListAdapter.OnMessageLongClickListener, MessageHolders.ContentChecker,
OnRemoteOperationListener, OnDatatransferProgressListener {

private OwnCloudClient mClient;
private Handler mHandler = new Handler();

@Override
protected void onViewBound(@NonNull View view) {

//................

   // Parse URI to the base URL of the Nextcloud server
   Uri serverUri = Uri.parse("https://cloud-beta.pupukkaltim.com/");
   // Create client object to perform remote operations

   mClient = OwnCloudClientFactory.createOwnCloudClient(
   serverUri,
   getActivity().getApplicationContext(),
   // Activity or Service context
   true);
   mClient.setCredentials(OwnCloudCredentialsFactory.newBearerCredentials(conversationUser.getToken()));
   //startReadRootFolder();
    startFolderCreation("myfolder");                      
   //Log.i("token", conversationUser.getToken());/

//................

private void startFolderCreation(String newFolderPath) {
    CreateFolderRemoteOperation createOperation = new CreateFolderRemoteOperation(newFolderPath, false);
    createOperation.execute(mClient, this, mHandler);
}

private void startReadRootFolder() {
    ReadFolderRemoteOperation refreshOperation = new ReadFolderRemoteOperation(FileUtils.PATH_SEPARATOR);
    // root folder
    refreshOperation.execute(mClient, this, mHandler);
}

@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
    if (operation instanceof CreateFolderRemoteOperation) {
        if (result.isSuccess()) {
            // do your stuff here
        }
    }
    // …
}

}

mClient.setCredentials(OwnCloudCredentialsFactory.newBearerCredentials(conversationUser.getToken()));

Why do use bearer credentials here?
Please try:
client.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials(username, conversationUser.getToken());

mClient.setCredentials(OwnCloudCredentialsFactory.newBearerCredentials(conversationUser.getToken()));

Why do use bearer credentials here?

which I know from references, if I want to use tokens, I must use this method:

// Set bearer access token
client.setCredentials(
    NextcloudCredentialsFactory.newBearerCredentials(accessToken)
);

Please try:
client.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials(username, conversationUser.getToken());

Thankyou very much, using your references, the function can work well.

mClient.setCredentials(OwnCloudCredentialsFactory.newBasicCredentials(currentUser.getUsername(), conversationUser.getToken()));

The whole bearer thing is only used for OAuth2.0., which we do not use anymore in our clients.
Rather than having every client to implement all auth methods, we let the server handle this in one central place and then get username/appToken back.

appToken is like a encrypted password and can be revoked individually from user settings.
I guess that naming similarity was confusing you.

Glad overall that it works now for you!