-
Notifications
You must be signed in to change notification settings - Fork 8
/
hubspot_oauth.R
146 lines (133 loc) Β· 3.72 KB
/
hubspot_oauth.R
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#' @return Named list
#' @keywords internal
#' @rdname hubspot-oauth
#' @examples
#' default_ld_hubspot_app()
#' @export
default_ld_hubspot_app <- function() {
list(
client_secret = "2c0b347e-1e2a-4db7-a31a-aaa0b2fcad13",
client_id = "5975ba5c-900c-4fd7-94dd-04df023e4263",
app_id = "205749",
scope = c(
"contacts", "forms"
),
optional_scope = c(
"content", "tickets",
"e-commerce"
)
)
}
#' Make a string about scope for the authorize URL
#' @noRd
make_scopes_string <- function(scope) {
glue::glue("{glue::glue_collapse(scope, sep = '%20')}")
}
#' Authorize URL for OAuth
#' @return Authorize URL (character)
#' @noRd
authorize_url <- function(app_info) {
if (is.null(app_info$optional_scope)) {
paste0(
"https://app.hubspot.com/oauth/authorize?client_id=",
app_info$client_id,
"&scope=",
make_scopes_string(app_info$scope)
)
} else {
paste0(
"https://app.hubspot.com/oauth/authorize?client_id=",
app_info$client_id,
"&scope=",
make_scopes_string(app_info$scope),
"&optional_scope=",
make_scopes_string(app_info$optional_scope)
)
}
}
#' Access URL for OAuth
#' @return Access URL (character)
#' @noRd
access_url <- function() {
"https://api.hubapi.com/oauth/v1/token"
}
#' Endpoint for OAuth
#' @return OAuth endpoint (httr special class)
#' @noRd
hubspot_oauth_endpoint <- function(app_info) {
endpoint <- httr::oauth_endpoint(
authorize = authorize_url(app_info),
access = access_url()
)
}
hubspot_oauth_app <- function(app_info) {
oauth_app <- httr::oauth_app(
appname = "MyHubSpotApp",
key = app_info$client_id,
secret = app_info$client_secret
)
}
#' Create/retrieve Hubspot authorization token.
#'
#' Functions related to creating or retrieving an OAuth 2.0
#' token for the Hubspot API.
#'
#' @includeRmd man/rmdhunks/oauth.Rmd
#' @includeRmd man/rmdhunks/auth.Rmd
#'
#' @param app_info A named list with client_secret, client_id, app_id,
#' required scope, optional scope (NULL if no additional scope).
#' @param set_renv Logical indicating whether to save the created token
#' as the default environment hubspot token variable. Defaults to TRUE,
#' meaning the token is saved to user's home directory as either the user
#' provided path, or
#' ".hubspot_token.rds" (or, if that already exists, then
#' .hubspot_token1.rds or .hubspot_token2.rds, etc.) and the path to the
#' token to said token is then set in the user's .Renviron file and re-
#' read to start being used in current active session.
#' @param token_path Path where to save the token. If `set_renv` is `FALSE`,
#' this is ignored.
#
#' @return
#' @family auth
#' @export
#'
#' @examples
#' \dontrun{
#' hubspot_token_create()
#' }
#' @rdname hubspot-oauth
hubspot_token_create <- function(app_info = default_ld_hubspot_app(),
set_renv = TRUE,
token_path = NULL) {
token <- httr::oauth2.0_token(
endpoint =
hubspot_oauth_endpoint(app_info),
app =
hubspot_oauth_app(app_info),
cache = FALSE
)
# from https://github.com/ropensci/rtweet/blob/1bd1e16d14df8b31a13a8c2f0e0ff0e87ea066d1/R/tokens.R#L219 # nolint
if (set_renv) {
if (is.null(token_path)) {
token_path <- uq_filename(file.path(home(), ".hubspot_token.rds"))
}
saveRDS(token, file = token_path, compress = FALSE)
set_renv("HUBSPOT_PAT" = token_path)
}
return(token)
}
#' @return Either NULL or the path in which the token is saved.
#' @export
#' @rdname hubspot-oauth
#'
#' @examples
#' hubspot_token_get()
hubspot_token_get <- function() {
token_path <- Sys.getenv("HUBSPOT_PAT")
if (token_path == "") {
return(NULL)
} else {
return(token_path)
}
}