/
index.js
75 lines (64 loc) · 2.32 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { cleanUserImage, cleanLocationImage } from '../utilities/helper/';
import { key } from '../hidden/hidden';
export const fetchUserImage = async (token) => {
const url = `https://api.pinterest.com/v1/users/me/?access_token=${token}&fields=image%2Cusername`;
const response = await fetch(url);
const data = await response.json();
const cleanedImage = await cleanUserImage(data);
return cleanedImage;
};
export const fetchDropBoard = async (token) => {
const url = `https://api.pinterest.com/v1/boards/deaddrops/dead-drops-official/pins/?access_token=${token}&fields=id%2Clink%2Cnote%2Curl%2Cimage`;
const response = await fetch(url);
const data = await response.json();
return data.data;
};
export const fetchLocation = async (coordinatesEntry) => {
try {
const { longitude, latitude } = coordinatesEntry;
const url = `https://maps.googleapis.com/maps/api/staticmap?zoom=13&size=300x300&maptype=roadmap&markers=color:blue%7Clabel:S%7C${latitude},${longitude}&key=${key}`;
const response = await fetch(url);
const data = await response.blob();
const cleanedImage = await cleanLocationImage(data);
const searchedLocation = {
long: longitude,
lat: latitude,
locationImage: cleanedImage,
};
return searchedLocation;
} catch (error) {
console.log(error.message);
}
};
export const postPin = async (note, token) => {
const image = 'https://postimg.cc/3dFYfCpr';
try {
const url = `https://api.pinterest.com/v1/pins/?access_token=${token}&fields=id%2Clink%2Cnote%2Curl&board=deaddrops/dead-drops¬e=${note}&image_url=${image}`;
const response = await fetch(url, {
method: 'POST',
body: JSON.stringify({
board: 'deaddrops/dead-drops',
note: note,
image_url: image
})
});
return response;
} catch (error) {
console.log(error.message);
}
};
export const fetchAccessToken = async (id, authcode) => {
const accessTokenUrl = 'https://api.pinterest.com/v1/oauth/token?' +
'grant_type=authorization_code&' +
'client_id=4987807426915878592&' +
`client_secret=${id}&` +
`code=${authcode}`;
const response = await fetch(accessTokenUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded '
}
});
const result = await response.json();
return result;
};