代码之家  ›  专栏  ›  技术社区  ›  Adnan

如何在Magento 1.9中使用OAuth 1.0a进行身份验证

  •  0
  • Adnan  · 技术社区  · 6 年前

    我想在我的Magento中的Angular应用程序中进行身份验证。我在StackOverflow中看不到任何帮助我解决问题的文档或问题!

    const consumerKey = 'c05028de10c242378cf5fd7eb7dd1008';
    const consumerSecret = '4350c23b9fda5f491ac5276218b3541d';
    const baseUrl = 'http://someSite.localhost';
    
    
    oauth = new HttpParams()
    .set('callback', 'http://127.0.0.1:4200/login')
    .set('consumer_key', consumerKey)
    .set('consumer_secret', consumerSecret);
    url = baseUrl + '/oauth/initiate';
    
    
    initiate() {
    return this.httpClient.post(this.url, this.oauth).subscribe(
      data => console.log(data)
    );
    }
    

    回应:

    oauth_problem=parameter_absent&oauth_parameters_absent=oauth_consumer_key
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Adnan    6 年前

    我通过以下方式解决了这个问题:

    1: 导入Crypto js包

    2: 创建签名

    3: 以秒为单位创建时间戳

    4: 为Nonce创建随机数

    5: Rest参数有一个静态值

    6: 使用URL中的所有这些参数发出请求


    import { Injectable } from '@angular/core';
    import { HttpClient, HttpResponse, HttpErrorResponse, HttpParams, HttpHeaders } from '@angular/common/http';
    import { catchError, tap } from 'rxjs/operators';
    import { throwError, Observable, of } from 'rxjs';
    import * as CryptoJS from 'crypto-js';
    
    
    
    
    
    @Injectable({
      providedIn: 'root'
    })
    
    //Making Huge request Parameters
    
    export class AuthService {
      consumerKey = 'c05028de10c242378cf5fd7eb7dd1008';
      consumerSecret = '4350c23b9fda5f491ac5276218b3541d';
      baseUrl = 'http://someSite.localhost';
      callback = 'http://127.0.0.1:4200/login/';
      signature_method = 'HMAC-SHA1';
      timestamp = Math.floor(Date.now() / 1000); // Timestamp in Second
      signature = CryptoJS.HmacSHA1('Lazurd', this.consumerKey);
      nonce = Math.random();
      httpOptions = {
        headers: new HttpHeaders({
          'Content-Type': 'multipart/form-data',
        })
      };
    
      // tslint:disable-next-line:max-line-length
      url = this.baseUrl + '/oauth/initiate?oauth_consumer_key=' + this.consumerKey
        + '&oauth_callback=' + this.callback
        + '&oauth_consumer_secret=' + this.consumerSecret
        + '&oauth_signature_method=' + this.signature_method
        + '&oauth_nonce=' + this.nonce
        + '&oauth_timestamp=' + this.timestamp
        + '&oauth_signature=' + this.signature;
    
      constructor(private httpClient: HttpClient) { }
      // Get Magento Token
      initiate() {
        return this.httpClient.post(this.url, '', this.httpOptions).subscribe(
      data => console.log(data)
        );
      }
    }