无法使用outlook API。节目[https://outlook.office365.com/api/v1.0/me 401(未经授权)]

时间:2017-07-18 07:02:03

标签: angular typescript

Error in developer tool 我正在尝试使用“outlook API”登录来创建一个供我办公室使用的网站。这是我使用的API:https://outlook.office365.com/api/v1.0/me

我已经在MVC中实现了,但不知道如何在Angular 2中实现

我的服务和组件代码:

outlook-email-check-services.ts

import { Injectable } from '@angular/core';
import { Http, RequestOptions, Headers, Response,
BaseRequestOptions, ResponseOptions, RequestMethod } from '@angular/http'; 
import {Observable} from 'rxjs/Observable';
import 'rxjs/Rx';

@Injectable()
export class OutlookEmailCheckService {
public token: string;
constructor(private http:Http) {
this.token = 'aaa@xyz.com';
}

private url = "https://outlook.office365.com/api/v1.0/me";

login(username: string, password: string): Observable<boolean> {
console.log('services');
return this.http.post(this.url, JSON.stringify({ username: username, password: password }))
.map((response: Response) => {

let token = response.json() && response.json().token;
console.log(token);
if (token) {

this.token = token;


localStorage.setItem('currentUser', JSON.stringify({ username: username, token: token }));

// return true to indicate successful login
return true;
} else {
// return false to indicate failed login
return false;
}
});
}

logout(): void {
// clear token remove user from local storage to log user out
this.token = null;
localStorage.removeItem('currentUser');
}
}

logincomponent.ts

import {Component,OnInit} from "@angular/core";
import {AuthorService} from './author.service';
import {FormBuilder, FormGroup, Validators, FormControl} from '@angular/forms';
import {CommonValidations} from './common/Common.validators';
import {OutlookEmailCheckService} from './outlook-email-check.service';

@Component({
templateUrl: './login.component.html',
})
export class LoginComponent implements OnInit {


constructor( private service:OutlookEmailCheckService) { 
}
ngOnInit() {

}

form = new FormGroup({
userName: new FormControl('',[
Validators.required,
Validators.minLength(5),
CommonValidations.IsvaliduserName,
]),

pwd: new FormControl('',Validators.required)
});
get userName(){
return this.form.get("userName");
}
get pwd(){
return this.form.get("pwd");
}


save(form){
console.log('login');
this.service.login(form.value.userName,form.value.pwd)//for getting data
.subscribe(result => {
if (result === true) {
// login successful

} else {
// login failed
// this.error = 'Username or password is incorrect';
// this.loading = false;
}
});

}

}

logincomponent.html

<form [formGroup]="form" (ngSubmit)="save(form)">

<div class="form-group">
<label for="userName">User Name</label>
<input
type="text"
id="userName"
class="form-control"
formControlName="userName"
required
>
<div *ngIf="!userName.valid && userName.touched" class="alert alert-danger">
<div class="alert alert-danger">Please Enter the Name</div>
<div class="alert alert-danger">Minimun lenght should be 5 </div>
<div *ngIf="userName.errors.IsvaliduserName" class="alert alert-danger"> 
UserNmae should not contain space</div>
<div *ngIf="userName.errors.IsAlreadyMember" class="alert alert-danger"> 
Not a member,plz Sign in!!</div>

</div>

</div>
<div class="form-group">
<label for="pwd">Password</label>
<input
type="text"
id="pwd"
class="form-control"
formControlName="pwd"
required>
<div class="alert alert-danger" *ngIf="!pwd.valid && pwd.touched">Please Enter the Password</div>

</div>
<button class="btn btn-primary" [disabled]=!form.valid type="submit">submit</button>

</form>

0 个答案:

没有答案