Nativescript OAuth2插件使用刷新令牌登录

Nativescript OAuth2插件使用刷新令牌登录,nativescript,nativescript-angular,refresh-token,nativescript-plugin,nativescript-oauth2,Nativescript,Nativescript Angular,Refresh Token,Nativescript Plugin,Nativescript Oauth2,我有一个移动应用程序,我正在尝试使用nativescript-oauth2插件和使用Azure B2C的自定义提供商对用户进行身份验证。我的要求是,我想让用户第一次使用他们的凭据登录到应用程序。用户成功登录后,我需要存储用户的刷新令牌,并在用户下次登录移动应用程序时使用这些存储的刷新令牌进行身份验证。使用刷新令牌,我想再次生成所有令牌 我曾尝试使用插件提供的refreshTokenWithCompletion()方法,但由于文档说明这是用于刷新access tokens()。我也对使用“refr

我有一个移动应用程序,我正在尝试使用nativescript-oauth2插件和使用Azure B2C的自定义提供商对用户进行身份验证。我的要求是,我想让用户第一次使用他们的凭据登录到应用程序。用户成功登录后,我需要存储用户的刷新令牌,并在用户下次登录移动应用程序时使用这些存储的刷新令牌进行身份验证。使用刷新令牌,我想再次生成所有令牌


我曾尝试使用插件提供的refreshTokenWithCompletion()方法,但由于文档说明这是用于刷新access tokens()。

我也对使用“refreshTokenWithCompletion”获取新的令牌结果有问题

下面是我的代码

import { TnsOAuthClient, ITnsOAuthTokenResult, configureTnsOAuth } from 'nativescript-oauth2';
import { AuthProvider } from './auth-provider.service';
import { TnsOaUnsafeProviderOptions } from 'nativescript-oauth2/providers/providers';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  private client: TnsOAuthClient = null;
  private customProvider: AuthProvider = null;

  private customProviderOptions: TnsOaUnsafeProviderOptions = {
    openIdSupport: "oid-none",
    clientId: "",
    redirectUri: "https://b2clogin.com/te/XXXXXXXXXXXXXX/XXXXXX_XXXXX_XXXXX/oauth2/authresp",
    scopes: ["openid https://XXXXXXXXXXXXXX.onmicrosoft.com/mobileapi/user_impersonation offline_access"],
    clientSecret: "",
    customQueryParams: {
      "p": "XXXXXX_XXXXX_XXXXX",
      "nonce": "defaultNonce",
      "response_mode": "query",
      "prompt": "login"
    }
};

  constructor() {
    this.customProvider = new AuthProvider(this.customProviderOptions);
    configureTnsOAuth([this.customProvider]);
   }

  public Login(providerType): Promise<ITnsOAuthTokenResult> {

    console.log('In Login');

    this.client = new TnsOAuthClient(providerType);

    return new Promise<ITnsOAuthTokenResult>((resolve, reject) => {
      this.client.loginWithCompletion((tokenResult: ITnsOAuthTokenResult, error) => {
          if (error) {
            console.error("back to main page with error: ");
            console.error(error);
            // reject(error);
          } else {
            console.log("back to main page with access token: ");
            console.log(tokenResult);
            // resolve(tokenResult);
          }
        });
    });
  }

  public Logout(): Promise<any> {

    return new Promise<any>((resolve, reject) => {
      if (this.client) {
        this.client.logoutWithCompletion((error) => {
            if (error) {
              console.error("back to main page with error: ");
              console.error(error);
              reject(error);
            } else {
              console.log("back to main page with success");
              resolve();
            }
          });
      }
      else {
        console.log("back to main page with success");
        resolve();
      }
    });

  }

  public RefreshAccess() {

    this.client.refreshTokenWithCompletion((tokenResult: ITnsOAuthTokenResult, error) => {
      if (error) {
        console.error("Unable to refresh token with error: ");
        console.error(error);
      } else {
        console.log("Successfully refreshed access token: ");
        console.log(tokenResult);
      }
    });

  }

}
import{TnsOAuthClient,itnsoauthttokenresult,configureTnsOAuth}来自“nativescript-oauth2”;
从“./auth provider.service”导入{AuthProvider};
从“nativescript-oauth2/providers/providers”导入{TnsOaUnsafeProviderOptions};
@注射的({
providedIn:'根'
})
导出类身份验证服务{
私有客户端:TnsOAuthClient=null;
私有customProvider:AuthProvider=null;
私有customProviderOptions:TnsOaUnsafeProviderOptions={
openIdSupport:“oid无”,
客户ID:“,
重定向URI:“https://b2clogin.com/te/XXXXXXXXXXXXXX/XXXXXX_XXXXX_XXXXX/oauth2/authresp",
作用域:[“openid”https://XXXXXXXXXXXXXX.onmicrosoft.com/mobileapi/user_impersonation 脱机访问“],
客户机密:“,
customQueryParams:{
“p”:“XXXXXX uXXXXX uXXXXX”,
“nonce”:“defaultNonce”,
“响应模式”:“查询”,
“提示”:“登录”
}
};
构造函数(){
this.customProvider=新的AuthProvider(this.customProviderOptions);
configureTnsOAuth([this.customProvider]);
}
公共登录(providerType):承诺{
log('In Login');
this.client=新的TnsOAuthClient(providerType);
返回新承诺((解决、拒绝)=>{
this.client.loginWithCompletion((令牌结果:ITnsOAuthTokenResult,错误)=>{
如果(错误){
console.error(“返回主页,出现错误:”);
控制台错误(error);
//拒绝(错误);
}否则{
log(“返回带有访问令牌的主页:”;
console.log(tokenResult);
//解决(结果);
}
});
});
}
公共注销():承诺{
返回新承诺((解决、拒绝)=>{
如果(此.client){
this.client.logoutWithCompletion((错误)=>{
如果(错误){
console.error(“返回主页,出现错误:”);
控制台错误(error);
拒绝(错误);
}否则{
console.log(“返回主页并成功”);
解决();
}
});
}
否则{
console.log(“返回主页并成功”);
解决();
}
});
}
公共访问{
this.client.refreshTokenWithCompletion((tokenResult:ITnsOAuthTokenResult,错误)=>{
如果(错误){
console.error(“无法刷新令牌,错误:”);
控制台错误(error);
}否则{
log(“已成功刷新访问令牌:”);
console.log(tokenResult);
}
});
}
}
如果有任何更正,请告诉我

错误:未捕获(承诺中):URIError:URI格式错误


我正在使用Azure AD B2C进行身份管理。

您能否详细说明所有其他代币的确切含义?访问令牌必须有助于您获得以前允许的任何内容。@Manoj,我指的是新的刷新令牌、id令牌和访问令牌。因此,当我获得刷新令牌时,我会将其存储在客户端,下次当用户想要登录到应用程序时,使用存储的刷新令牌并将其发送到Azure B2C并获取新的访问和刷新令牌..所有这些我都希望使用nativescript oauth-2插件实现。同样可以通过postmany从
itnsoauthttokenresult
对象获取所有这些值,这是
refreshttokenwithcompletion
方法回调的参数。你试过了吗?如果是,您缺少哪一个?