Thursday, September 27, 2007

Google Checkout : Failed to Get Basic Authentication Headers

Google Checkout : Send failed with code: 401. Response body was: Failed to Get Basic Authentication Headers
The most common problem facing php file in Google Checkout is ResponseHandler.php. Many of us really don't know how exactly its working and behave. After few days of my r&d in Google Checkout i have the solution to fix the "Get Basic Authentication Headers" problem.

I started running ResponseHandler.php against Google Checkout running under different situations and I ran into a problem with authentication when PHP was being run as a CGI under Apache. When running as a server module (mod_php) PHP takes care of decoding HTTP basic for you (see HTTP basic authentication in PHP). When a using HTTP basic PHP will automatically populate $_SERVER[’PHP_AUTH_USER’] and $_SERVER[’PHP_AUTH_PW’] variables with the username and password that were provided. IF and ONLY IF PHP is being run as a server module (like mod_php). If you are running PHP as a CGI then those two variables won’t get created at all, ever, even when using HTTP basic authentication. And since you can’t do anything in WordPress via AtomPub without authenticating you are dead in the water. Well, not exactly.

However, there is a workaround available which can make HTTP Auth for PHP working even when in CGI mode.

1. Create one .htaccess file in the folder where your responsehandler.php file is :

.htaccess file:

<<<Start--- AuthName "Google checkout Basic Authentication" AuthType Basic AuthUserFile D:\\\\www\\\\webroot\\\\projectfolder\\\\googlelibraryforcheckout/.htpasswd
require valid-user
---End>>>


2. Create one .htpasswd file in the folder where your responsehandler.php file is :
.htpasswd
$merchant_id:$merchant_key


and now you need to disable few codes from googleresponse.php file :

* googlecheckout/responsehandler.php folowing code will be disabled if
* CGI config is set to True
*
*
[CODE]
//Parse the HTTP header to verify the source.
if(isset($HTTP_SERVER_VARS['PHP_AUTH_USER']) && isset($HTTP_SERVER_VARS['PHP_AUTH_PW'])) {
$compare_mer_id = $HTTP_SERVER_VARS['PHP_AUTH_USER'];
$compare_mer_key = $HTTP_SERVER_VARS['PHP_AUTH_PW'];
}
else {
error_func("HTTP Basic Authentication failed.\n");
exit(1);
}

if($compare_mer_id != $merchant_id || $compare_mer_key != $merchant_key) {
error_func("HTTP Basic Authentication failed.\n");
exit(1);
}
[/CODE]


I hope this article will help you alot to work arround the Google Checkout's "Send failed with code: 401. Response body was: Failed to Get Basic Authentication Headers"

Regards,
Prashant Agarwal
(If you have any problem then please write here i will try to solve all queries related to php and google / paypal chekckout)

10 comments:

ropu said...

Hi
it seems you are using a very old version of the Application. Latest PHP Sample code has much more functionalities regarding Basic auth handling. In fact to call the HTTP authentication there is a new method in the googlereponse class, HttpAuthentication()

if you are using Zencart or OSC in the admin area there is a config to disable this, and a script to create the htacces.
have a look at this article:

http://www.ropusoft.com.ar/hack_book/htaccess.php

please refer to the official support forums for more info:
http://groups.google.com/group/google-checkout-api-php/topics
http://groups.google.com/group/google-checkout-oscommerce-zencart/topics

Hope this helps

ropu, main developer of the PHP Sample code, OSC and Zencart integration for Google Checkout.

Mathew said...

Wow what a beautiful solution prashant!! Really its really what i was searching but was not getting any where...

Thnaks alot for this work!!

Is this possible for you to integrate google checkout for my ecommerce website?

Prashant Agarwal said...

Yes i can,
Contact me in my email id: prashcom@gmail.com

Cornerstone Websites said...

What code do I change in the 1.4.7 version of the responsehandler.php? I am having a terrible time of this. The automated script in the latest version of Zen-Cart never worked for me.

Albion Soft Guys said...

Ecommerce solutions are available in plentiful supply online, and it can often be a somewhat difficult task to figure which solution actually works best for you. In order to give yourself the best possible chance of selecting an appropriate application, it is important to weigh up and consider exactly what you require from your ecommerce application. If you are just starting out in business, or have very basic ecommerce needs, it might be a good idea to opt for an online payment processor. http://www.infyecommercesolution.com/

inventmagic said...

htaccess file:
Remove stars...
*************************
AuthType Basic
AuthName "Google checkout Basic Authentication"
AuthUserFile path to your file
*************************

Password file:
Remove stars...

*************************
$merchant_id:Encrypted Merchant_key
*************************

You must encrypt your merchant_key/ password in order for the password file to parse correctly (at least on my PHP installation) Here link to a deeper description of the htaccess process


http://httpd.apache.org/docs/1.3/howto/htaccess.html

inventmagic said...

Thanks for the info by the way...

梦中林 said...

When the Wow Gold wolf finally found the Buy Wow Goldhole in the chimney he crawled wow gold cheap down and KERSPLASH right into that kettle of water and that was cheapest wow gold the end of his troubles with the big bad wolf.
The next day the cheap wow gold little pig invited his mother over . She said "You see it is just as mygamegoldI told you. The way to get along in the world is to do world of warcraft gold things as well as you can." Fortunately for that little pig, he buy cheap wow gold learned that lesson. And he just k4gold lived happily ever after!

seo.media said...

Won't you wonder if somebody says he doesn't know what is a software company ? Am sure you will fall off from your chair. But believe me it is true. There still are people in third world countries who don’t know anything about software development, web application development or internet. So those who are not really aware about these buzzing things, try to find out what is a tech-world of Internet.

Vicky said...

Hi Prashant,

First of all i would like to know that in .htpasswd file i have to write excalty "$merchant_id:$merchant_key" or i have to give values of merchant id and merchant key.

I just wrote "$merchant_id:$merchant_key" as it is. but then it is giving me following error:

Kindly go through it and revert back.. thanks in advance

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, support@supportwebsite.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.



--------------------------------------------------------------------------------

Apache/1.3.33 Server at www.brightone.com Port 80

Hire Me

The On Demand Global Workforce - oDesk

Prashant Agarwal

Prashant Agarwal
Prashant Agarwal

About Me

My Photo
Prashant Agarwal
"I am Prashant Agarwal, I have 4 Years of experience in Programming. I love to do coding but now a days I am only managing things in a very big Organization in Kolkata India. Now I am working as a Project Manager. My role is to handle entire programming, designing & SEO team. I really love my job. I am enjoing & learning every day in my life. "
View my complete profile