<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic What is the best way to use credentials for API calls from databricks notebook? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/what-is-the-best-way-to-use-credentials-for-api-calls-from/m-p/5557#M1932</link>
    <description>&lt;P&gt;Hello, I have an Databricks account on Azure, and the goal is to compare different image tagging services from Azure, GCP, AWS via corresponding API calls, with Python notebook. I have problems with GCP vision API calls, specifically with credentials: as far as I understand, the one necessary step is to set 'GOOGLE_APPLICATION_CREDENTIALS' environment variable in my databricks notebook with something like&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;os.environ['GOOGLE_APPLICATION_CREDENTIALS'] ='/folder1/credentials.json'&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;where '/folder1/credentials.json' is the place my notebook looks for json file with credentials (notebook is in the same folder,&amp;nbsp;/folder1/notebook_api_test). &lt;/P&gt;&lt;P&gt;I am getting this path by looking into&amp;nbsp;&lt;B&gt;Workspace&lt;/B&gt;-&amp;gt;&amp;nbsp;&lt;B&gt;Copy file path&lt;/B&gt;&amp;nbsp;in the Databricks web page. But this approach doesn't work, when cell is executed, I am getting this error:&lt;/P&gt;&lt;P&gt;&lt;B&gt;DefaultCredentialsError: File /folder1/credentials.json was not found.&lt;/B&gt;&lt;/P&gt;&lt;P&gt;What is the right way to deal with credentials to access google vision API from Azure Databricks notebook?&lt;/P&gt;</description>
    <pubDate>Wed, 19 Apr 2023 14:21:58 GMT</pubDate>
    <dc:creator>lugger1</dc:creator>
    <dc:date>2023-04-19T14:21:58Z</dc:date>
    <item>
      <title>What is the best way to use credentials for API calls from databricks notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/what-is-the-best-way-to-use-credentials-for-api-calls-from/m-p/5557#M1932</link>
      <description>&lt;P&gt;Hello, I have an Databricks account on Azure, and the goal is to compare different image tagging services from Azure, GCP, AWS via corresponding API calls, with Python notebook. I have problems with GCP vision API calls, specifically with credentials: as far as I understand, the one necessary step is to set 'GOOGLE_APPLICATION_CREDENTIALS' environment variable in my databricks notebook with something like&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;os.environ['GOOGLE_APPLICATION_CREDENTIALS'] ='/folder1/credentials.json'&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;where '/folder1/credentials.json' is the place my notebook looks for json file with credentials (notebook is in the same folder,&amp;nbsp;/folder1/notebook_api_test). &lt;/P&gt;&lt;P&gt;I am getting this path by looking into&amp;nbsp;&lt;B&gt;Workspace&lt;/B&gt;-&amp;gt;&amp;nbsp;&lt;B&gt;Copy file path&lt;/B&gt;&amp;nbsp;in the Databricks web page. But this approach doesn't work, when cell is executed, I am getting this error:&lt;/P&gt;&lt;P&gt;&lt;B&gt;DefaultCredentialsError: File /folder1/credentials.json was not found.&lt;/B&gt;&lt;/P&gt;&lt;P&gt;What is the right way to deal with credentials to access google vision API from Azure Databricks notebook?&lt;/P&gt;</description>
      <pubDate>Wed, 19 Apr 2023 14:21:58 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/what-is-the-best-way-to-use-credentials-for-api-calls-from/m-p/5557#M1932</guid>
      <dc:creator>lugger1</dc:creator>
      <dc:date>2023-04-19T14:21:58Z</dc:date>
    </item>
    <item>
      <title>Re: What is the best way to use credentials for API calls from databricks notebook?</title>
      <link>https://community.databricks.com/t5/data-engineering/what-is-the-best-way-to-use-credentials-for-api-calls-from/m-p/5558#M1933</link>
      <description>&lt;P&gt;Ok, here is a trick: in my case, the file with GCP credentials is stored in notebook workspace storage, which is not visible to&lt;B&gt; os.environ()&lt;/B&gt; command. &lt;/P&gt;&lt;P&gt;So solution is to read a content of this file, and save it to the cluster storage attached to the notebook, which is created with the cluster and is erased when cluster is gone (so we need to repeat this procedure every time the cluster is re-created). According to &lt;A href="https://docs.databricks.com/files/workspace-interact.html" alt="https://docs.databricks.com/files/workspace-interact.html" target="_blank"&gt;this&amp;nbsp;link&lt;/A&gt;, we can read the content of the credentials json file stored in notebook workspace with&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;with open('/Workspace/folder1/cred.json'): #note that I need a full path here, for some reason
content = f.read()&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and then according to&amp;nbsp;&lt;A href="https://docs.databricks.com/files/index.html" alt="https://docs.databricks.com/files/index.html" target="_blank"&gt;his doc,&lt;/A&gt;, we need to save it on another place in a new file (with the same name in my case, cred.json), namely on cluster storage attached to the notebook (which is visible to os-related functions, like os.environ()), with&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;fd = os.open("cred.json", os.O_RDWR|os.O_CREAT)
ret = os.write(fd,content.encode())  
#need to add .encode(), or will get TypeError: a bytes-like object is required, not 'str'
os.close(fd)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Only after that we can continue with setting an environment variable, required for GCP authentication:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;os.environ['GOOGLE_APPLICATION_CREDENTIALS'] ='./cred.json'&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and then API calls should work fine, without DefaultCredentialsError.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Apr 2023 23:21:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/what-is-the-best-way-to-use-credentials-for-api-calls-from/m-p/5558#M1933</guid>
      <dc:creator>lugger1</dc:creator>
      <dc:date>2023-04-20T23:21:00Z</dc:date>
    </item>
  </channel>
</rss>

