IBM Cloud Docs
获取令牌

获取令牌

当用户或后端服务与应用程序交互时,他们可能需要获得授权才能执行特定操作。App ID 验证发出请求的实体是否已获得授权,并将访问令牌和身份令牌返回到应用程序。 如果发出请求的实体是最终用户,那么令牌可能包含有关用户的信息,例如用户的许可权作用域和用户名。 如果发出请求的是后端服务,那么仅返回访问令牌。

使用 GUI 获取客户机标识和密钥

要获取令牌,您必须有自己的客户 ID 和密码。 凭证是每个应用程序特有的,用于帮助识别和验证令牌可能分配给的用户。

  1. 导航至 App ID 仪表板的应用程序选项卡。

  2. 如果已列出一组凭证,那么可以跳至步骤 3。 如果没有,请创建一个。

    1. 应用程序选项卡上,单击添加应用程序
    2. 为应用程序提供名称,然后单击保存以返回到已注册应用程序的列表。 应用程序的名称不能超过 50 个字符。
  3. 从已注册应用程序的列表中,选择要使用的应用程序。 相应行将展开以显示凭证。

  4. 复制客户端标识和私钥。

使用 API 获取客户机标识和私钥

要获取令牌,您必须有自己的客户 ID 和密码。 凭证特定于每个应用程序,用于帮助识别和验证可能向其分配了令牌的用户。

  1. /management/v4/<tenantID>/applications 端点 发出 POST 请求。

    请求:

    curl -X POST https://us-south.appid.cloud.ibm.com/management/v4/39a37f57-a227-4bfe-a044-93b6e6060b61/applications/ \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer <IAMToken>' \
    -d '{"name": "ApplicationName"}'
    

    示例响应:

    {
       "clientId": "c90830bf-11b0-4b65-bffe-9773f8703bad",
       "tenantId": "b42f7429-fc24-48ds-b4f9-616bcc31cfd5",
       "secret": "YWQyNjdkZjMtMGRhZC00ZWRkLThiOTQtN2E3ODEyZjhkOWQz",
       "name": "testing",
       "oAuthServerUrl": "https://us-south.appid.cloud.ibm.com/oauth/v4/b42f7429-fc24-48ds-b4f9-616bcb31cfd5",
       "profilesUrl": "https://us-south.appid.cloud.ibm.com",
       "discoveryEndpoint": "https://us-south.appid.cloud.ibm.com/oauth/v4/b42f7429-fc24-48ds-b4f9-616bcb31cfd5/.well-known/openid-configuration"
    }
    
  2. 复制客户端标识和私钥。

使用 GUI 获取访问令牌和身份令牌

有了客户端 ID 和秘密,就可以通过使用应用程序接口或 SDK 获取访问和身份令牌。 以下示例显示如何使用“资源所有者密码”(ROP) 流来获取令牌。

此操作只能通过 API 完成。 要查看步骤,请切换到 API 指示信息。

使用 API 获取访问令牌和身份令牌

有了客户端 ID 和秘密,就可以通过使用应用程序接口或 SDK 获取访问和身份令牌。 以下示例显示如何使用“资源所有者密码”(ROP) 流来获取令牌。

  1. 从凭证中获取租户标识、客户端标识、私钥和 OAuth 服务器 URL。

  2. 使用 base64 编码器对客户机标识和密钥进行编码。

  3. 使用以下代码示例来检索令牌。 您用来获取令牌的授权类型可能会有所不同,这取决于您正在使用的授权类型。 有关选项的详细列表,请查看 swagger 文档

    curl -X POST 'https://<region>.appid.cloud.ibm.com/oauth/v4/<tenantID>/token' \
    -H 'Authorization: Basic base64Encoded{<clientID>:<clientSecret>}' \
    -H 'Accept: application/json' \
    -F 'grant_type=password' \
    -F 'username=testuser@test.com' \
    -F 'password=testuser'
    
    // iOS Swift example
    
    class delegate : TokenResponseDelegate {
       public func onAuthorizationSuccess(accessToken: AccessToken?, identityToken: IdentityToken?, refreshToken: RefreshToken?, response:Response?) {
       //User authenticated
       }
    
       public func onAuthorizationFailure(error: AuthorizationError) {
       //Exception occurred
       }
    }
    
    AppID.sharedInstance.signinWithResourceOwnerPassword(username: username, password: password, delegate: delegate())
    
    AppID.getInstance().signinWithResourceOwnerPassword(getApplicationContext(), username, password, new TokenResponseListener() {
       @Override
       public void onAuthorizationFailure (AuthorizationException exception) {
          //Exception occurred
       }
    
       @Override
       public void onAuthorizationSuccess (AccessToken accessToken, IdentityToken identityToken, RefreshToken refreshToken) {
          //User authenticated
       }
    });
    
    // Declare the API you want to protect
    app.get("/api/protected",
    
       passport.authenticate(APIStrategy.STRATEGY_NAME, {
       session: false
       }),
       function(req, res) {
       // Get full appIdAuthorizationContext from request object
       var appIdAuthContext = req.appIdAuthorizationContext;
    
       appIdAuthContext.accessToken; // Raw access_token
       appIdAuthContext.accessTokenPayload; // Decoded access_token JSON
       appIdAuthContext.identityToken; // Raw identity_token
       appIdAuthContext.identityTokenPayload; // Decoded identity_token JSON
       appIdAuthContext.refreshToken; // Raw refresh_token
       ...
       }
    );
    
    // Server-side swift example
    
    let options = [
       "clientId": "<clientID>",
       "secret": "<secret>",
       "tenantId": "<tenantID>",
       "oauthServerUrl": "<oauthServerURL>",
       "redirectUri": "<appURL>" + CALLBACK_URL
    ]
    let webappKituraCredentialsPlugin = WebAppKituraCredentialsPlugin(options: options)
    let kituraCredentials = Credentials()
    kituraCredentials.register(plugin: webappKituraCredentialsPlugin)