How can I use the IMREAD function with a URL that requires basic authentication in MATLAB?
12 views (last 30 days)
Show older comments
MathWorks Support Team
on 17 Aug 2009
Edited: MathWorks Support Team
on 21 Jun 2021
I am using the IMREAD function to import an image into MATLAB. However, to access the URL for the image I need to log on using basic authentication.
Accepted Answer
MathWorks Support Team
on 15 Jun 2021
Edited: MathWorks Support Team
on 21 Jun 2021
The ability to use basic authentication with the IMREAD command for a URL is not available in MATLAB. The IMREAD function internally uses the URLWRITE to create a image file. Hence as a workaround, you can try to modify the URLWRITE command to perform basic authentication. Basic Authentication is a specific authorization mechanism for web servers which is not secure. Not all web servers allow this type of authentication, so this work around may not apply to you.
The following workaround has not been tested and therefore may require modification to get them to work in your situation. This implements a hard coded username and passwords into a copy of the URLWRITE function. You will need to understand the basic authentication scheme in order to apply this workaround.
References for more information are listed below.
1. Save a copy of the file $MATLABROOT\toolbox\matlab\iofun\urlwrite.m as
$MATLABROOT\toolbox\matlab\iofun\urlwrite_basicauth.m (where $MATLABROOT is the MATLAB root
directory on your machine, as returned by typing:
matlabroot
at the MATLAB command prompt.)
2. Open the file urlwrite_basicauth and add a line to add the basic authentication property to the URL
connection. Around line 76 you should see the following section of code:
% Create a urlConnection.
[urlConnection,errorid,errormsg] = urlreadwrite(mfilename,urlChar);
if isempty(urlConnection)
if catchErrors, return
else error(errorid,errormsg);
end
end
Immediately after this add the following code and save the changes.
urlConnection.setRequestProperty( 'Authorization','Basic YWRtaW46YWRtaW4=');
3. Execute the following command at the MATLAB command prompt
rehash toolboxcache
4. Determine the encoded user name and password required for your authentication. Then subsitute your encoded username and password for "YWRtaW46YWRtaW4=" in code added in step 2. The encoding scheme is explained here: (following the link to "base-64" for more details):
5. Now save a copy of the file $MATLABROOT\toolbox\matlab\imagesci\imread.m as
$MATLABROOT\toolbox\matlab\imagesci\imread_basicauth.m
6. Open the file imread_basicauth and replace the that uses the URLWRITE function with URLWRITE_BASICAUTH. Around line 338 you should see the following line of code:
filename = urlwrite(filename, tempname);
Replace this with :
filename = urlwrite_basicauth(filename, tempname);
0 Comments
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!