- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个带有 Angular 和 Cordova 插件的 Android 应用程序,我想将它与 Google 身份验证集成。
我已经安装了 cordova-plugin-googleplus并且我已成功集成到应用程序中。
当用户登录时,我会收到一个响应,我可以在其中获取 accessToken、配置文件用户信息和 refreshToken。
现在我想实现一个功能来刷新 token ,而不会每小时用一个新的提示屏幕打扰用户。
我已经成功更新了 accessToken,但它只能在第一次使用
我用过这两种方式:
curl -X POST \
'https://oauth2.googleapis.com/token?code=XXXXXXXXXXXXXXXX&client_id=XXXXXXXXXXXXXXXX.apps.googleusercontent.com&client_secret=YYYYYYYYYYYY&grant_type=authorization_code' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded'
4/rgFU-hxw9QSbfdj3ppQ4sqDjK2Dr3m_YU_UMCqcveUgjIa3voawbN9TD6SVLShedTPveQeZWDdR-Sf1nFrss1hc
1/FTSUyYTgU2AG8K-ZsgjVi6pExdmpZejXfoYIchp9KuhtdknEMd6uYCfqMOoX2f85J
curl -X POST \
'https://oauth2.googleapis.com/token?code=1/FTSUyYTgU2AG8K-ZsgjVi6pExdmpZejXfoYIchp9KuhtdknEMd6uYCfqMOoX2f85J&client_id=XXXXXXXXXXXXXXXX.apps.googleusercontent.com&client_secret=YYYYYYYYYYYY&grant_type=authorization_code' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded'
{
"error" : "invalid_grant",
"error_description" : "Malformed auth code."
}
最佳答案
最后,我根据需要刷新了访问 token 。问题是对 Google Api 的工作方式的误解。
第一次更新 token 时,需要使用这些参数调用此端点,并将从同意屏幕调用(serverAuthCode)的响应中获得的值设置为{{refreshToken}}
https://oauth2.googleapis.com/token?code={{refreshToken}}&client_id={{googleClientId}}&client_secret={{googleClientSecret}}&grant_type=authorization_code
https://oauth2.googleapis.com/token?refresh_token={{tokenUpdated}}&client_id={{googleClientId}}&client_secret={{googleClientSecret}}&grant_type=refresh_token
import { Injectable} from '@angular/core';
import { Router } from '@angular/router';
import { GooglePlus } from '@ionic-native/google-plus/ngx';
@Injectable({
providedIn: 'root'
})
export class AuthenticationService {
static AUTH_INFO_GOOGLE = 'auth-info-google';
static CLIENT_ID = 'XXXXX-XXXX.apps.googleusercontent.com';
static CLIENT_SECRET = 'SecretPasswordClientId';
public authenticationState = new BehaviorSubject(false);
constructor(
private router: Router,
private googlePlus: GooglePlus) {
}
public isAuthenticated() {
return this.authenticationState.value;
}
public logout(): Promise<void> {
this.authenticationState.next(false);
return this.googlePlus.disconnect()
.then(msg => {
console.log('User logged out: ' + msg);
}, err => {
console.log('User already disconected');
});
}
/**
* Performs the login
*/
public async login(): Promise<any> {
return this.openGoogleConsentScreen().then(async (user) => {
console.log(' ServerAuth Code: ' + user.serverAuthCode);
user.updated = false;
await this.setData(AuthenticationService.AUTH_INFO_GOOGLE, JSON.stringify(user));
this.authenticationState.next(true);
// Do more staff after successfully login
}, err => {
this.authenticationState.next(false);
console.log('An error ocurred in the login process: ' + err);
console.log(err);
});
}
/**
* Gets the Authentication Token
*/
public async getAuthenticationToken(): Promise<string> {
return this.getAuthInfoGoogle()
.then(auth => {
if (this.isTokenExpired(auth)) {
return this.refreshToken(auth);
} else {
return 'Bearer ' + auth.accessToken;
}
});
}
private async openGoogleConsentScreen(): Promise<any> {
return this.googlePlus.login({
// optional, space-separated list of scopes, If not included or empty, defaults to `profile` and `email`.
'scopes': 'profile email openid',
'webClientId': AuthenticationService.CLIENT_ID,
'offline': true
});
}
private isTokenExpired(auth: any): Boolean {
const expiresIn = auth.expires - (Date.now() / 1000);
const extraSeconds = 60 * 59 + 1;
// const extraSeconds = 0;
const newExpiration = expiresIn - extraSeconds;
console.log('Token expires in ' + newExpiration + ' seconds. Added ' + extraSeconds + ' seconds for debugging purpouses');
return newExpiration < 0;
}
private async refreshToken(auth: any): Promise<any> {
console.log('The authentication token has expired. Calling for renewing');
if (auth.updated) {
auth = await this.requestGoogleRefreshToken(auth.serverAuthCode, auth.userId, auth.email);
} else {
auth = await this.requestGoogleAuthorizationCode(auth.serverAuthCode, auth.userId, auth.email);
}
await this.setData(AuthenticationService.AUTH_INFO_GOOGLE, JSON.stringify(auth));
return 'Bearer ' + auth.accessToken;
}
private getAuthInfoGoogle(): Promise<any> {
return this.getData(AuthenticationService.AUTH_INFO_GOOGLE)
.then(oauthInfo => {
return JSON.parse(oauthInfo);
}, err => {
this.clearStorage();
throw err;
});
}
private async requestGoogleAuthorizationCode(serverAuthCode: string, userId: string, email: string): Promise<any> {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/x-www-form-urlencoded');
let params: HttpParams = new HttpParams();
params = params.set('code', serverAuthCode);
params = params.set('client_id', AuthenticationService.CLIENT_ID);
params = params.set('client_secret', AuthenticationService.CLIENT_SECRET);
params = params.set('grant_type', 'authorization_code');
const options = {
headers: headers,
params: params
};
const url = 'https://oauth2.googleapis.com/token';
const renewalTokenRequestPromise: Promise<any> = this.http.post(url, {}, options).toPromise()
.then((response: any) => {
const auth: any = {};
auth.accessToken = response.access_token;
console.log('RefreshToken: ' + response.refresh_token);
auth.serverAuthCode = response.refresh_token;
auth.expires = Date.now() / 1000 + response.expires_in;
auth.userId = userId;
auth.email = email;
auth.updated = true;
return auth;
}, (error) => {
console.error('Error renewing the authorization code: ' + JSON.stringify(error));
return {};
});
return await renewalTokenRequestPromise;
}
private async requestGoogleRefreshToken(serverAuthCode: string, userId: string, email: string): Promise<any> {
let headers = new HttpHeaders();
headers = headers.set('Content-Type', 'application/x-www-form-urlencoded');
let params: HttpParams = new HttpParams();
params = params.set('refresh_token', serverAuthCode);
params = params.set('client_id', AuthenticationService.CLIENT_ID);
params = params.set('client_secret', AuthenticationService.CLIENT_SECRET);
params = params.set('grant_type', 'refresh_token');
const options = {
headers: headers,
params: params
};
const url = 'https://oauth2.googleapis.com/token';
const renewalTokenRequestPromise: Promise<any> = this.http.post(url, {}, options).toPromise()
.then((response: any) => {
const auth: any = {};
auth.accessToken = response.access_token;
console.log('RefreshToken: ' + serverAuthCode);
auth.serverAuthCode = serverAuthCode;
auth.expires = Date.now() / 1000 + response.expires_in;
auth.userId = userId;
auth.email = email;
auth.updated = true;
return auth;
}, (error) => {
console.error('Error renewing refresh token: ' + JSON.stringify(error));
return {};
});
return await renewalTokenRequestPromise;
}
private setData(key: string, value: any): Promise<any> {
console.log('Store the value at key entry in the DDBB, Cookies, LocalStorage, etc')
}
private getData(key: string): Promise<string> {
console.log('Retrieve the value from the key entry from DDBB, Cookies, LocalStorage, etc')
}
private clearStorage(): Promise<string> {
console.log('Remove entries from DDBB, Cookies, LocalStorage, etc related to authentication')
}
}
关于cordova - 在第二次尝试刷新 token 时如何修复 "Malformed auth code"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58209700/
主要问题是可以从已保存的 Auth 对象设置零 Auth 对象: let oldAuth = Auth.auth().currentUser let oldAuthId = Auth.auth().c
何时使用 this.afAuth.auth.signInWithCredential 在我的 Ionic 应用程序中一切正常。我可以使用 Google 身份验证登录并将用户个人资料推送到 fireba
我想在启动时使用我的测试用户帐户预加载 firebase auth 模拟器,就像我对 Firestore 模拟器及其导入/导出选项所做的一样。我尝试在模拟器运行时使用 auth:import 和 au
我正在尝试通过创建 Firebase DataService 来 DRY 我的应用程序,但我不知道为 Auth.auth() 转换什么类型。我查看了源代码定义,但它在我不理解的 Objective C
我有一个更新用户个人资料图片的组件:。我有第二个组件检测用户状态的变化:。问题是,在执行updateProfile()之后,没有触发auth.onAuthStateChanged()。因此,我得到了老
我正在使用 Facebook Javascript SDK,并且正在调整我网站的用户登录系统。我想知道 auth.login/auth.logout 和 auth.sessionChange 之间有什
目前,我正在完成Daniel Kehoe的Learn Ruby on Rails教程。练习之一是使用Google的Gmail帐户从“联系人”页面发送联系人表格。 但是,当我发送联系表格时,没有在邮箱中
我在我的应用中使用 Firebase Auth。我更新电子邮件如下: firebaseAuth.currentUser?.updateEmail(email) 电子邮件正在 100% 更新(必要时我也
在我的应用程序中,当我第一次让用户加入时,我会让他们登录并确认这有效,并且他们会获得一个用户 ID。但稍后在我的 Storyboard 中,我调用了 Auth.auth.currentUser.uid
我是编程新手,我正在尝试从 firebase 数据库中读取数据。我从 Firebase 手册中得到这段代码,我想知道术语 uid,AUTH.auth() 在这里是什么意思。它是一个变量吗?我从 fir
已结束。此问题不符合 Stack Overflow guidelines .它目前不接受答案。 我们不允许提出有关书籍、工具、软件库等方面的建议的问题。您可以编辑问题,以便用事实和引用来回答它。 关闭
背景 我构建了一个连接到 Authorize.net 支付网关的电子商务应用程序。管理员可以通过以下方式处理信用卡: 管理员立即从客户的信用卡中扣款的“捕获”交易。 “仅授权”交易,管理员从信用卡中授
我正在使用 dj-rest-auth ( https://dj-rest-auth.readthedocs.io/en/latest/ ) 并尝试实现自定义注册表单。当我尝试注册新用户时,我有基本表单
我们正在使用 Azure Web App Easy Auth,并使用 Web App 作为反向代理,将流量转发到 Angular 应用程序。 角度应用程序使用/.auth/me 并使用 token 并
我有一个 Controller 和一个如下所示的 View ,它在一段时间内工作完美,但是在向服务器发出一些请求后(即每秒重新加载一次),它将在此行失败 if (!$this->tank_auth-
我希望在单个 Web 应用程序中,部分部分使用身份验证,部分部分完全开放(或者更具体地说,不使用基于容器的身份验证)。 使用基于容器的身份验证的应用程序部分位于 URL /而打开的部分位于 URL /
只要我按下按钮调用 signOut 方法,当前 View Controller 就会被关闭。但是,注销过程是成功的。 (我正在使用 FirebaseAuth SDK。) 这是我的代码: @IBActi
对于Firebase iOS,如果我想做用户认证,这3个选择有什么区别吗? FirebaseUI FirebaseUI/授权 Firebase/授权 如果有人可以解释使用“预构建”FirebaseUI
我正在使用 Vue-auth 包来验证我的 vue-cli 应用程序。一切正常,但当我在我的 vue 操作中访问此代码时,它返回 undefined。有什么想法吗? const AuthService
所以我正在使用 Laravel 身份验证,第一次它工作正常,但删除 Auth 文件夹后,我再次运行 php artisan make:auth 但它不会创建另一个 Auth 文件夹。 View 和模型
我是一名优秀的程序员,十分优秀!