Make WordPress API Call Using RCurl/HTTR
<p>I am looking to make an API call to grab the post data of a protected WP blog. I am having some trouble translating the WP authentication procedure from PHP to R, I think in part because I don't fully understand the process.</p>

<p>My understanding of the OAuth token receipt procedure comes from <a href="https://developer.wordpress.com/docs/oauth2/" rel="nofollow">this page</a>:</p>

<ol>
<li>Send user to an authorization endpoint that prompts a login:
<code>https://public-api.wordpress.com/oauth2/authorize?client_id=your_client_id&amp;redirect_uri=your_url&amp;response_type=code&amp;blog=1234</code></li>
<li>Then make a <code>POST</code> request at the API with the code in the above redirect url incorporated:</li>
</ol>

<p><code>$curl = curl_init('https://public-api.wordpress.com/oauth2/token');
   curl_setopt( $curl, CURLOPT_POST, true );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, array(
        'client_id' =&gt; your_client_id,
        'redirect_uri' =&gt; your_redirect_url,
        'client_secret' =&gt; your_client_secret_key,
        'code' =&gt; $_GET['code'], // The code from the previous request
        'grant_type' =&gt; 'authorization_code'
    ) );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
    $auth = curl_exec( $curl );
    $secret = json_decode($auth);
    $access_key = $secret-&gt;access_token;</code></p>

<p>which then returns:</p>

<pre><code>{
    "access_token": "YOUR_API_TOKEN",
    "blog_id": "blog ID",
    "blog_url": "blog url",
    "token_type": "bearer"
}
</code></pre>

<p>but I have no idea where to start in r. I apologize that this is not a reproducible problem. I tried to use the code <a href="https://github.com/hadley/httr/blob/master/demo/oauth2-azure.r" rel="nofollow">here</a>, but I can't quite figure out what went wrong:</p>

<pre><code>app_name &lt;- 'myapp' 
client_id &lt;- 'your_client_id'
redirect_uri &lt;- 'your_redirect_url'
client_secret &lt;- 'your_client_secret_key'
resource_uri &lt;- #IDK what this is

oauth_endpoint(authorize = "https://public-api.wordpress.com/oauth2/authorize?client_id=your_client_id&amp;redirect_uri=your_url&amp;response_type=code&amp;blog=1234",
access = "https://public-api.wordpress.com/oauth2/token")
wordpress_endpoint &lt;- oauth_endpoints('wordpress')

# Create the app instance.
myapp &lt;- oauth_app(key = 'your_client_id',
                   secret = 'your_client_secret_key')

mytoken &lt;- oauth2.0_token(wordpress_endpoint, myapp,
                          user_params = list(resource = resource_uri),
                          use_oob = FALSE)
</code></pre>