【问题标题】:Required parameter is missing: grant_type while accessing google token in ionic2缺少必需的参数:在 ionic2 中访问 google 令牌时的 grant_type
【发布时间】:2023-03-22 18:14:02
【问题描述】:

缺少必需参数:在 ionic2 中访问 google 令牌时的 grant_type

错误:-

{
  "error" : "invalid_request",
  "error_description" : "Required parameter is missing: grant_type"
}

代码

    let creds=JSON.stringify({
        'client_id':'251059024984-8113pdtb11gm8m4evdq59stlpvcab8cn.apps.googleusercontent.com',
        'client_secret':'uVWZt_Vd5mMLXGQDo7lmAIUe',
        'redirect_uri':'http://localhost/callback',
        'grant_type':'authorization_code',
        'code':data
    });
    var headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    console.log(data)


    this.http.post('https://accounts.google.com/o/oauth2/token',creds,{headers: headers}).map(res => res.json()).subscribe(data => {
        console.log(data)
    },(error)=>{
        console.log('error in', error);
    })

当我在 Postman 中检查运行完美的 api 时

【问题讨论】:

  • 尝试删除 json.stringify 并只留下 {}

标签: angular ionic-framework ionic2


【解决方案1】:
let creds = 'client_id=251059024984-8113pdtb11gm8m4evdq59stlpvcab8cn.apps.googleusercontent.com'
    +'&client_secret=uVWZt_Vd5mMLXGQDo7lmAIUe'
    +'&redirect_uri=http://localhost/callback'
    +'&grant_type=authorization_code'
    +'&code=' + data

JSON.stringify() 格式不正确

【讨论】:

【解决方案2】:

login.ts

onSubmit() {
    console.log("hkjfghjksfgd")
    this.showLoader();
    this.authService.login(this.loginData).subscribe(
        (data) => {
        this.loading.dismiss();
        this.result = data;
        console.log(this.result)
        localStorage.setItem('token', this.result.access_token);
        }, (err) => {
        this.loading.dismiss();
    });
}

services.ts

login(data: any): Observable<any> {
    let body = JSON.stringify(
        'ClientId = 5'+'&grant_type = password'
     );
    let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    let options = new RequestOptions({ headers: headers });
    console.log(data)
    return this.http.post(this.logindetail, body, options) // ...using post request
        .map((res: Response) => res.json())
        .catch(this.handleError);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 2016-07-07
    • 1970-01-01
    • 2012-06-27
    • 2014-09-04
    相关资源
    最近更新 更多