<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://sugarclub.sugarai.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Accessing Sugar Oauth Keys in PHP</title><link>https://sugarclub.sugarai.com/dev-club/f/questions-answers/6905/accessing-sugar-oauth-keys-in-php</link><description>I am creating a dashlet to access Box files and folder in an Account record. I need to authenticate to Box&amp;#39;s API in my php code, but I don&amp;#39;t want to hardcode the credentials. What is the best way to secure my Box API credentials? I am using SugarCloud</description><dc:language>en-US</dc:language><generator>Telligent Community 12</generator><item><title>RE: Accessing Sugar Oauth Keys in PHP</title><link>https://sugarclub.sugarai.com/thread/30364?ContentTypeID=1</link><pubDate>Tue, 21 Nov 2023 17:17:02 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:9a4fa66b-d62d-4731-81a9-58f649131655</guid><dc:creator>Anthony Watson</dc:creator><description>&lt;p&gt;Rather than doing a direct db call, you should leverage the module, which for that table is `OAuthKeys`.&amp;nbsp; You would need to store the actual ID of the record in your code to avoid having to query by name. So assuming you have the ID, it would be:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="php"&gt;function get_creds() {
    $oauthID = &amp;quot;some-id-here&amp;quot;;
    $oauthKey = \BeanFactory::getBean(&amp;quot;OAuthKeys&amp;quot;, $oauthID, [&amp;#39;strict_retrieve&amp;#39; =&amp;gt; true]);
    if(!empty($oauthKey)) {
        $credentials = [$oauthKey-&amp;gt;c_key, $oauthKey-&amp;gt;c_secret];
    }
    return $credentials;
}
&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Alternatively, you could look it up by the key, since this is often the thing that is known. So if you stored it as `box_api_user`, you could use:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="php"&gt;function get_creds() {
    $oauthKeyName = &amp;quot;box_api_user&amp;quot;;
    $oauthKey = \OAuthKey::fetchKey($oauthKeyName);
    if(!empty($oauthKey)) {
        $credentials = [$oauthKey-&amp;gt;c_key, $oauthKey-&amp;gt;c_secret];
    }
    return $credentials;
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Accessing Sugar Oauth Keys in PHP</title><link>https://sugarclub.sugarai.com/thread/30358?ContentTypeID=1</link><pubDate>Tue, 21 Nov 2023 04:41:38 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:50a65468-4ac1-4ba6-821a-60014cbd89e1</guid><dc:creator>Enrico Simonetti</dc:creator><description>&lt;p&gt;&lt;span class="emoticon" data-url="https://sugarclub.sugarai.com/cfs-file/__key/system/emoji/1f631.svg" title="Scream"&gt;&amp;#x1f631;&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Accessing Sugar Oauth Keys in PHP</title><link>https://sugarclub.sugarai.com/thread/30343?ContentTypeID=1</link><pubDate>Fri, 17 Nov 2023 19:17:27 GMT</pubDate><guid isPermaLink="false">5c521d64-519d-47a6-9065-134618b211bf:8e951867-4b25-4b8a-a5ca-b20289c62272</guid><dc:creator>Greg Billings</dc:creator><description>&lt;p&gt;I found my own answer. with the ID and secret in Oauth Keys, I can get it from php with this:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;function get_creds() {
global $db;
$credentials = [];
$query = &amp;quot;SELECT c_key, c_secret FROM oauth_consumer WHERE name = &amp;#39;box_api_for_dashlet&amp;#39;&amp;quot;;
$result = $db-&amp;gt;query($query);
if ($row = $db-&amp;gt;fetchByAssoc($result)) {
$credentials = [$row[&amp;#39;c_key&amp;#39;], $row[&amp;#39;c_secret&amp;#39;]];
}
return $credentials;
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>