我一直试图找到一个有用的库来使用oauth2与通用提供程序进行身份验证 . 我是Ionic的新手,并且找不到可以帮助我轻松完成这项工作的图书馆 . 我也看到大多数库仅限于移动设备,所以我决定采取艰难的方式并尝试自己编写代码 . 如果代码运气不好或非常错误,我道歉 . 很久以前我已经完成了一些PHP编程,我正在尝试用这个来编程 .
我首先使用以下命令生成了一个提供程序页面:ionic g provider MakeHttpRequest . 这是我到目前为止所写的内容,只是获取令牌的第一步 .
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
/*
Generated class for the MakeHttpRequest provider.
See https://angular.io/docs/ts/latest/guide/dependency-injection.html
for more info on providers and Angular 2 DI.
*/
@Injectable()
export class MakeHttpRequest {
constructor
(
public http: Http,
private client_name : string,
private client_id : string,
private client_secret : string,
private end_point : string,
private auth_uri : string,
private redirect_uri : string
) {
console.log('Hello MakeHttpRequest Provider');
this.client_name = 'Alex';
this.client_id = 'lalala';
this.client_secret = 'lalalalala';
this.end_point = 'http://www.lalaland.com';
this.auth_uri = 'https://services.lalaland.com/oauth/authorize';
this.redirect_uri = 'http://localhost/callback';
}
public getcode(){
this.http.get("${this.auth_uri}?client_id=${this.client_id}&redirect_uri=${this.redirect_uri}&response?type=code")
.map(res => res.json()).subscribe(data => {
console.log(data);
});
}
}
然后将如下类导入home.ts:
import {MakeHttpRequest} from '../../providers/make-http-request';
在home.ts下创建了构造函数,如下所示:constructor(public navCtrl:NavController,private platform:Platform,private MakeHttpRequest:MakeHttpRequest)
并创建了以下功能:
public button(){
this.MakeHttpRequest.getcode()
}
然后将按钮功能添加到home.html页面 .
但我得到的只是一个错误 . 有人可以告诉我,我是在正确的方向,甚至是关闭 . 任何帮助或评论都非常感谢 .
1 回答
Can't resolve all parameters for MakeHttpRequest: (Http, ?, ?, ?, ?, ?, ?)
对于这个问题,你需要通过angular dependency injection
由于MakeHttpRequest提供程序是通过DI创建的,因此构造函数的所有参数都由DI设置 . 它能够定位http但不能设置基元类型 . 你必须删除字符串参数并在
getCode()
中设置