forked from STAT545-UBC/STAT545-UBC-original-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bit003_api-key-env-var.html
129 lines (105 loc) · 6.13 KB
/
bit003_api-key-env-var.html
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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>Store an API key as an environment variable</title>
<script src="libs/jquery-1.11.0/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link href="libs/bootstrap-2.3.2/css/united.min.css" rel="stylesheet" />
<link href="libs/bootstrap-2.3.2/css/bootstrap-responsive.min.css" rel="stylesheet" />
<script src="libs/bootstrap-2.3.2/js/bootstrap.min.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<link rel="stylesheet"
href="libs/highlight/default.css"
type="text/css" />
<script src="libs/highlight/highlight.js"></script>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs && document.readyState && document.readyState === "complete") {
window.setTimeout(function() {
hljs.initHighlighting();
}, 0);
}
</script>
<link rel="stylesheet" href="libs/local/nav.css" type="text/css" />
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
</style>
<div class="container-fluid main-container">
<header>
<div class="nav">
<a class="nav-logo" href="index.html">
<img src="static/img/stat545-logo-s.png" width="70px" height="70px"/>
</a>
<ul>
<li class="home"><a href="index.html">Home</a></li>
<li class="faq"><a href="faq.html">FAQ</a></li>
<li class="syllabus"><a href="syllabus.html">Syllabus</a></li>
<li class="topics"><a href="topics.html">Topics</a></li>
<li class="people"><a href="people.html">People</a></li>
</ul>
</div>
</header>
<div id="header">
<h1 class="title">Store an API key as an environment variable</h1>
</div>
<p>Problem: You’re using an API and you need to provide your key or token to authenticate yourself. You don’t want to do this interactively and repeatedly. You don’t want to store the key in your scripts or push it to GitHub by mistake.</p>
<p>Solution: Store it as an environment variable and retrieve it with <code>Sys.getenv()</code> in an R script or an RMarkdown document.</p>
<p>Motivating example: I use this method to interact with GitHub via the <a href="https://github.com/cscheid/rgithub"><code>github</code> R package</a>, a wrapper around the Github v3 API.</p>
<p>Source: The <a href="https://github.com/hadley/httr"><code>httr</code> package</a> has a vignette, <a href="http://cran.r-project.org/web/packages/httr/vignettes/api-packages.html">Best practices for writing an API package</a>, that advocates this approach (but lacks this excruciating detail).</p>
<p>How to do it:</p>
<ul>
<li><p>Create the username, password, key, token or whatever secret thing your API demands. Copy it to the clipboard.</p>
<ul>
<li>Example: To use the GitHub API, you must create a personal access token in the <a href="https://github.com/settings/applications">Applications area</a> of your GitHub personal settings.</li>
</ul></li>
<li><p>Identify your home directory. Not sure? Enter <code>normalizePath("~/")</code> in the R console.</p></li>
<li><p>Create a new text file. If in RStudio, do <em>File > New File > Text file.</em></p></li>
<li><p>Put a line like this for the key you wish to store:</p>
<pre><code>GITHUB_TOKEN=blahblahblahblahblahblah</code></pre>
<p>where <code>GITHUB_TOKEN</code> is a name relevant to the API you’re accessing and <code>blahblahblahblahblahblah</code> is your actual key.</p></li>
<li><p>Put a line break at the end! If you’re using an editor that shows line numbers, there should be two lines, where the second one is empty.</p></li>
<li><p>Save in your home directory with the filename <code>.Renviron</code>. If questioned, YES you do want to use a filename that begins with a dot <code>.</code>.</p></li>
<li><p>Note that by default <a href="http://linux.about.com/cs/linux101/g/dot_file.htm">dotfiles</a> are usually hidden. But within RStudio, the file browser will make <code>.Renviron</code> visible and therefore easy to edit in the future.</p></li>
<li><p>Restart R! <code>.Renviron</code> is processed only <a href="http://stat.ethz.ch/R-manual/R-patched/library/base/html/Startup.html">at the start of an R session</a>.</p></li>
<li><p>Use <code>Sys.getenv()</code> to access your API-key-stored-as-environment-variable and provide to your API wrapper of choice. For example, here’s how I use my <code>GITHUB_TOKEN</code> with the <code>github</code> package:</p>
<pre><code>library(github)
ctx <- create.github.context(access_token = Sys.getenv("GITHUB_TOKEN"))
# ... proceed to use other package functions to open issues, etc.</code></pre>
<p>The details of how your API wrapper wants you to provide your key will vary by the package but hopefully this helps!</p></li>
</ul>
<p><em>FAQ: Why define this environment variable via <code>.Renviron</code> instead of in <code>.bash_profile</code> or <code>.bashrc</code>?</em></p>
<p>Because there are many combinations of OS and ways of running R where the <code>.Renviron</code> approach “just works” and the bash stuff does not. When R is a child process of, say, Emacs or RStudio, you can’t always count on environment variables being passed to R. Put them in an R-specific start-up file and save yourself some grief.</p>
<div class="footer">
This work is licensed under the <a href="http://creativecommons.org/licenses/by-nc/3.0/">CC BY-NC 3.0 Creative Commons License</a>.
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
$(document).ready(function () {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>