0

I am working on an Angular app, where I get a list of albums and photos using the Flickr API. Below is the serviced I created to consume the Flickr API

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { GlobalsService } from './globals.service';
import * as Rx from 'rxjs';

interface Response {
  photosets: PhotoSets;
}

interface PhotoSets {
  page: number;
  pages: number;
  photoset: PhotoSet[];
  length: number;
}

interface PhotoSet {
  id: string;
  description: String;
  title: String;
}

interface String {
  _content: string;
}

interface PhotoSetByIds {
  photoset: PhotoSetById;
}

interface PhotoSetById {
  id: string;
  owner: string;
  ownername: string;
  page: number;
  pages: number;
  per_page: number;
  perpage: number;
  photo: Photo[];
  primary: string;
  title: string;
  total: string;
}

interface Photo {
  length: number;
  farm: number;
  id: string;
  isfamily: number;
  isfriend: number;
  ispramy: string;
  ispublic: number;
  secret: string;
  server: string;
  title: string;
}

@Injectable({
  providedIn: 'root'
})
export class FlickrServiceService {
  constructor(private http: HttpClient, private globals: GlobalsService) {
  }

  getPhotoSets() {
    return this.http.get<Response>(
      'https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=' +
        this.globals.apiKey +
        '&user_id=' +
        this.globals.userId +
        '&format=json&nojsoncallback=1'
    );
  }

  getPhotos(photosetId: any) {
    return this.http.get<PhotoSetByIds>(
      'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=' +
        this.globals.apiKey +
        '&photoset_id=' +
        photosetId +
        '&user_id=' +
        this.globals.userId +
        '&format=json&nojsoncallback=1'
    );
  }
}

The thing is, I get the following CORS error when I try to get something:

Access to XMLHttpRequest at 'https://api.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=&user_id=&format=json&nojsoncallback=1' from origin 'http://localhost:4200' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response. core.js:4002 ERROR HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: "https://api.flickr.com/services/rest/?method=flick…ser_id=170538248@N08&format=json&nojsoncallback=1", ok: false, …}

I still don't know how to solve this problem. Any help? Thank you!

Thilina Koggalage
  • 1,044
  • 8
  • 16
José Paulino
  • 61
  • 1
  • 5

1 Answers1

1

Import HttpClientModule, HttpClientJsonpModule in app.module.ts

import { HttpClientModule, HttpClientJsonpModule } from '@angular/common/http';

@NgModule({
  imports: [HttpClientJsonpModule, HttpClientModule]
})
export class AppModule { }

In you FlickrService you should use jsoncallback instead of nojsoncallback.

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class PhotoService {

imagesUrl = `https://api.flickr.com/services/....../format=json&jsoncallback=JSONP_CALLBACK`;

  constructor(private http: HttpClient) { }

  getPhotos() {
    return  this.http.jsonp(this.imagesUrl, 'JSONP_CALLBACK');
  }

}

Finally in your component,

import { FlickrService } from './flickr.service';
export class PhotosComponent  {

  photoArray: any[];

  constructor(private flickrService: FlickrService) { 
  }

  ngOnInit() {
    this.flickrService.getPhotos()
      .subscribe((res: any) => this.photoArray = res.items);
  }

}

Hope this will help you.

Thilina Koggalage
  • 1,044
  • 8
  • 16