FORUMS

Obtaining Authentication Information for Huawei Drive Kit

981 posts
Thanks Meter: 1,057
 
By XDARoni, XDA Community Manager on 15th May 2020, 05:06 PM
Post Reply Email Thread
Obtaining Authentication Information

Use Case
HUAWEI ID is required for a user uses to access and manage their files in Drive. Before your app can use the Drive SDK, it needs to prompt the user to sign in with their HUAWEI ID. After the user signs in, your app saves relevant user information for calling Drive SDK APIs later.

Service Process
The figure below shows the basic service process.


Development Procedure
Step 1 Integrate the HMS SDK.

To use the Drive SDK, your app needs to obtain user information through the HMS SDK first. For details, please refer to Integrating the Drive SDK and HMS SDK.

Step 2 Set the HMS scope.

Set the Drive scope by calling the HuaweiIdAuthParamsHelper.setScopeList API provided by the HMS SDK to obtain the permission to access Drive APIs.
Code:
List<Scope> scopeList = new LinkedList<>();
scopeList.add(HuaweiIdAuthAPIManager.HUAWEIID_BASE_SCOPE);// Basic account permissions
scopeList.add(new Scope(DriveScopes.SCOPE_DRIVE));// All permissions, except permissions for the app folder.
scopeList.add(new Scope(DriveScopes.SCOPE_DRIVE_FILE));// Permissions to view and manage files.
scopeList.add(new Scope(DriveScopes.SCOPE_DRIVE_METADATA));// Permissions to view and manage file metadata, excluding file content.
scopeList.add(new Scope(DriveScopes.SCOPE_DRIVE_METADATA_READONLY));// Permissions only for viewing file metadata, excluding file entities.
scopeList.add(new Scope(DriveScopes.SCOPE_DRIVE_READONLY));// Permissions to view file metadata and content.
scopeList.add(new Scope(DriveScopes.SCOPE_DRIVE_APPDATA));// Permissions to view and manage app files.
HuaweiIdAuthParams params = new HuaweiIdAuthParamsHelper(HuaweiIdAuthParams.DEFAULT_AUTH_REQUEST_PARAM)
         .setAccessToken()
         .setIdToken()
         .setScopeList(scopeList) 
         .createParams();
The access permissions vary according to the Drive scope. You may apply for the permissions as needed. For details, please refer to Signing In with HUAWEI ID.

Step 3 Store authentication information.

After obtaining the user information, save the unionId and AccessToken of the user for calling the Drive SDK whenever needed. The following provides an example implementation method. You may use another implementation method if necessary.

Sample code:

Creates the CredentialManager class.
Code:
/** 
* Credential management class 
* <p> 
* since 1.0 
*/ 
public class CredentialManager { 
     /** 
     * Drive authentication information. 
     */ 
     private DriveCredential mCredential; 
     private CredentialManager() { 
     } 
     private static class InnerHolder { 
         private static CredentialManager sInstance = new CredentialManager(); 
     } 
    /** 
     * Singleton CredentialManager instance.
     * 
     * @return CredentialManager 
     */ 
    public static CredentialManager getInstance() { 
         return InnerHolder.sInstance; 
     }
    /** 
     * Initialize Drive based on the context and HUAWEI ID information including unionId, countrycode, and accessToken.
     * When the current accessToken expires, register an AccessMethod and obtain a new accessToken.
     * 
     * @param unionID   unionID in HwID
     * @param at        access token 
     * @param refreshAT Callback function for refreshing the access token.
     */ 
    public int init(String unionID, String at, DriveCredential.AccessMethod refreshAT) { 
         if (StringUtils.isNullOrEmpty(unionID) || StringUtils.isNullOrEmpty(at)) { 
             return DriveCode.ERROR; 
         } 
         DriveCredential.Builder builder = new DriveCredential.Builder(unionID, refreshAT); 
         mCredential = builder.build().setAccessToken(at); 
         return DriveCode.SUCCESS; 
     } 
    /** 
     * Obtain DriveCredential.
     * 
     * @return DriveCredential 
     */ 
    public DriveCredential getCredential() { 
         return mCredential; 
     } 
     /** 
     * Exit Drive and clear all cache information generated during use. 
     */ 
     public void exit(Context context) { 
         // Clear cache files. 
         deleteFile(context.getCacheDir()); 
         deleteFile(context.getFilesDir()); 
     }
    /** 
     * Delete cache files. 
     * 
     * @param file Designated cache file 
     */ 
     private static void deleteFile(java.io.File file) { 
         if (null == file || !file.exists()) { 
             return; 
         } 
         if (file.isDirectory()) { 
             java.io.File[] files = file.listFiles(); 
             if (files != null) { 
                 for (java.io.File f : files) { 
                     deleteFile(f); 
                 } 
             } 
         } 
     } 
 }
Call initDrive in the onActivityResult callback after the user signs in.
Code:
private static DriveCredential.AccessMethod refreshAT = new DriveCredential.AccessMethod() {
    @Override
    public String refreshToken() {
        // Obtain a new access token through the HMS SDK.
        return refreshAccessToken();
    }
};
private void initDrive() {
    // signInHuaweiId is the HUAWEI ID that the user used to sign in. Based on signInHuaweiId, obtain unionId and AccessToken.
    final String unionID = signInHuaweiId.getUnionId();

    final String accessToken = signInHuaweiId.getAccessToken();





    int returnCode = CredentialManager.getInstance().init(unionID, accessToken, refreshAT);
    if (DriveCode.SUCCESS == returnCode) {
        //Jump to the app home page after successful initialization.
        jumpToMainActivity();
    }
}
Post Reply Subscribe to Thread

Guest Quick Reply (no urls or BBcode)
Message:
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes