OAuthService
When the mini program requests access to specific scopes (resources or capabilities), the IAPMiniProgram SDK calls the OAuthService SPI to request the super app to obtain user authorization and generate authorization codes for the mini program. For details on how the SPI functions, refer to the OAuthService workflow.
This SPI provides the following three methods:
getAuthCode
IAPMiniProgram calls the getAuthCode
method to request the super app to generate authorization codes for the mini program.
Method signature
getAuthCode(
clientId: string,
scopes: Array<string>,
oauthFlowType: OAuthCodeFlowType,
extendedInfo: Map<string, string>,
apiContext: APIContext
): Promise<OAuthResult>;
Request parameters
Field | Data type | Required | Description |
clientId | string | Yes | The unique ID that is assigned by Mini Program Platform to identify a mini program. |
scopes | Array<string> | Yes | The authorization scopes, which represent the resources or capabilities that are requested by the mini program. For more information about the valid values, refer to the Scopes section. |
oauthFlowType | OAuthCodeFlowType | Yes | The type of the mini program. Valid values are:
|
extendedInfo | Map<string, string> | No | An extended attribute that is used to provide additional information if necessary. |
apiContext | APIContext | Yes | A context object, which carries the mini program runtime metadata. |
Response parameters
Field | Data type | Required | Description |
result | Promise<OAuthResult> | Yes | The result of the |
showAuthPage
The super app calls the showAuthPage
method to display an authorization dialog to the user when the requested scopes require the user's agreement before being granted to mini programs.
Method signature
showAuthPage(
clientId: string,
name: string,
logo: string,
scopes: Array<string>,
apiContext: APIContext,
extendedInfo?: Map<string, string>
): Promise<OAuthPageConfirmResult>;
Request parameters
Field | Data type | Required | Description |
clientId | string | Yes | The unique ID that is assigned by Mini Program Platform to identify a mini program. |
scopes | Array<string> | Yes | The authorization scopes that require the user's agreement. The values fall within those specified in the scopes request parameter of |
name | string | Yes | The name of the mini program that requests the scopes. |
logo | string | No | The logo URL of the mini program that requests the scopes. |
extendedInfo | Map<string, string>? | No | An extended attribute that is used to provide additional information if necessary. |
apiContext | APIContext | Yes | A context object, which carries the mini program runtime metadata. |
Response parameters
Field | Data type | Required | Description |
result | Promise<OAuthPageConfirmResult> (extends BaseServiceResult) | Yes | The result of the |
getAuthorizedScopes
IAPMiniProgram calls this method to retrieve a list of scopes that the super app supports for granting to mini programs.
Method signature
getAuthorizedScopes(
clientId: string,
extendedInfo: Map<string, string>,
apiContext: APIContext
): Promise<OAuthScopeQueryResult>;
Request parameters
Field | Data type | Required | Description |
clientId | string | Yes | The unique ID that is assigned by Mini Program Platform to identify a mini program. |
extendedInfo | Map<string, string> | No | An extended attribute that is used to provide additional information if necessary. |
apiContext | APIContext | Yes | A context object, which carries the mini program runtime metadata. |
Response parameters
Field | Data type | Required | Description |
result | Promise<OAuthScopeQueryResult> | Yes | The result of the |
Scopes
The following table lists the scopes that are defined by Mini Program Platform:
Scope | Description |
auth_base | The unique ID that is assigned by the super app to identify a user. Note:
|
auth_user | The basic user profile information, including the user ID, avatar, etc. Note:
|
Error codes
Error code | Error message | |
1000 | ERROR_CODE_UNKNOWN_ERROR | Unknown error |
1001 | ERROR_CODE_USER_CANCEL | The user cancels the operation. |
1002 | ERROR_CODE_APP_SERVICE_ERROR | The app service is wrong. |
1003 | ERROR_CODE_TIMEOUT | Timeout |
2001 | ERROR_CODE_AUTH_PENDING_AGREEMENT | Authorization is not finished or is pending. |
1005 | ERROR_CODE_SYSTEM_ERROR | System error |
Samples
The following samples show how to implement the OAuthService SPI to generate authorization codes for mini programs:
- Create a class that implements the OAuthService interface.
- Implement the
getAuthorizedScopes
method to provide a list of the supported scopes. - Implement the
getAuthCode
method to generate authorization codes for the mini program. - Implement the
showAuthPage
method to show the user authorization dialog.
The samples differ depending on whether to open a React Native page during the SPI call:
Open a React Native page
import type {
OAuthCodeFlowType,
APIContext,
OAuthResult,
OAuthScopeQueryResult,
OAuthPageConfirmResult,
BaseError
} from 'iapminiprogram-rn';
import { BASE_SERVICE_ERROR_CODE } from 'iapminiprogram-rn';
import type { OAuthService } from 'iapminiprogram-rn';
class TestOAuthService implements OAuthService {
async getAuthCode(
clientId: string,
scopes: Array<string>,
oauthFlowType: OAuthCodeFlowType,
extendedInfo: Map<string, string>,
apiContext: APIContext
): Promise<OAuthResult> {
return new Promise<OAuthResult>(function (resolve) {
const result: OAuthResult = {
showReactNativePage: true,
bundleURLPath: "XXX",//the bundle name
jsMainModulePath: "XXX",//the registerComponent js name
moduleName: "XXX",//the string in AppRegistry.registerComponent()
};
resolve(result);
});
}
async getAuthorizedScopes(
clientId: string,
extendedInfo: Map<string, string>,
apiContext: APIContext
): Promise<OAuthScopeQueryResult> {
return new Promise<OAuthScopeQueryResult>(function (resolve) {
const result: OAuthScopeQueryResult = {
showReactNativePage: true,
bundleURLPath: "XXX",
jsMainModulePath: "XXX",
moduleName: "XXX",
};
resolve(result);
});
}
async showAuthPage(
clientId: string,
name: string,
logo: string,
scopes: Array<String>,
apiContext: APIContext,
extendedInfo?: Map<String, String>
): Promise<OAuthPageConfirmResult> {
return new Promise<OAuthPageConfirmResult>(function (resolve) {
const result: OAuthPageConfirmResult = {
showReactNativePage: true,
bundleURLPath: "XXX",
jsMainModulePath: "XXX",
moduleName: "XXX",
};
resolve(result);
});
}
}
Remain inside the mini program
import type {
OAuthCodeFlowType,
APIContext,
OAuthResult,
OAuthScopeQueryResult,
OAuthPageConfirmResult,
BaseError
} from 'iapminiprogram-rn';
import type { OAuthService } from 'iapminiprogram-rn';
class TestOAuthService implements OAuthService {
async getAuthCode(
clientId: string,
scopes: Array<string>,
oauthFlowType: OAuthCodeFlowType,
extendedInfo: Map<string, string>,
apiContext: APIContext
): Promise<OAuthResult> {
return new Promise<OAuthResult>(function (resolve) {
const result: OAuthResult = {
authCode: 'XXX'
};
resolve(result);
});
}
async getAuthorizedScopes(
clientId: string,
extendedInfo: Map<string, string>,
apiContext: APIContext
): Promise<OAuthScopeQueryResult> {
return new Promise<OAuthScopeQueryResult>(function (resolve) {
const result: OAuthScopeQueryResult = {
authorizedScopes: ['auth_base', 'auth_user'],
};
resolve(result);
});
}
async showAuthPage(
clientId: string,
name: string,
logo: string,
scopes: Array<String>,
apiContext: APIContext,
extendedInfo?: Map<String, String>
): Promise<OAuthPageConfirmResult> {
return new Promise<OAuthPageConfirmResult>(function (resolve) {
const result: OAuthPageConfirmResult = {};
resolve(result);
});
}
}