Contacts API - 401 Unauthorized

1,138 views
Skip to first unread message

Wes Plybon

unread,
Mar 21, 2008, 1:52:52 PM3/21/08
to Google Contacts API
I keep getting a 401 Unauthorized error when I try requesting a feed.

I have successfully authenticated, and have received a session token
which is tmpSession. I'm trying to access contact information and,
for now, have the username hard coded in the url. Am I setting the
token propertly, or am I missing something? Here's the C# code where
the issue is.

gService = new Service("cp", "whipplehill-contactTest-1");
gService.SetAuthenticationToken(tmpSession);

Response.Write(gService.QueryAuthenticationToken());

FeedQuery q = new FeedQuery();
q.Uri = new Uri("http://www.google.com/m8/feeds/contacts/
whipt...@gmail.com/base");

AtomFeed f = gService.Query(q);

Thanks!

Joe Cascio, Jr.

unread,
Mar 21, 2008, 1:56:07 PM3/21/08
to google-co...@googlegroups.com
I'm not sure the API is working yet with the actual email addresses. Try "default" instead of the email address.

JC

Joe Cascio, Jr.

unread,
Mar 21, 2008, 1:57:05 PM3/21/08
to google-co...@googlegroups.com

Wes Plybon

unread,
Mar 21, 2008, 2:03:57 PM3/21/08
to Google Contacts API
Ah. That's good to know.

Trying that didn't work, but I'll leave it at default. For future
attempts.

Thanks
--Wes

On Mar 21, 1:57 pm, "Joe Cascio, Jr." <joec0...@gmail.com> wrote:
> In other words,
>
> q.Uri = new Uri("http://www.google.com/m8/feeds/contacts/default/base");<http://www.google.com/m8/feeds/contacts/>
>
> On Fri, Mar 21, 2008 at 1:56 PM, Joe Cascio, Jr. <joec0...@gmail.com> wrote:
> > I'm not sure the API is working yet with the actual email addresses. Try
> > "default" instead of the email address.
>
> > JC
>

Wes Plybon

unread,
Mar 21, 2008, 3:49:07 PM3/21/08
to Google Contacts API
I believe my problem lies with AuthSub. When I switched to
ClientLogin I'm able to make calls like normal. Has anyone else had
any issues using AuthSub with the .NET libraries in an ASP.NET
application?

Walnut

unread,
Mar 26, 2008, 2:59:32 PM3/26/08
to Google Contacts API
Guys,

I am hitting the same issue. Not sure whether there is something
fundamental wrong with my code (quite possibly!).

string result = string.Empty;
HttpWebRequest request = null;
string email = "myemail";
string url = string.Format("http://www.google.com/m8/
feeds/contacts/{0}/base HTTP/1.1", Server.UrlEncode(email));
Uri uri = new Uri(url);
request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
request.ContentType = "application/x-www-form-
urlencoded";
request.Headers.Add("Authorization",
string.Format("AuthSub token='{0}'", googleContactsToken));
request.UserAgent = "Java/1.5.0_06";
request.Accept = "text/html, image/gif, image/jpeg, *;
q=.2, */*; q=.2";
request.KeepAlive = true;
request.GetResponse();

.Net does not allow you set the host header on an HTTP request which
may be an issue also I am using single quotes to surround the token as
when escaping double quotes the escape sequence is sent rather than
the result of it, i.e. \"<token>\" rather than "<token>".

Any ideas where I am going wrong?

Ta

Sebastian Kanthak

unread,
Mar 26, 2008, 7:14:14 PM3/26/08
to google-co...@googlegroups.com
Did you verify your domain and agreed to the Tos on
https://www.google.com/accounts/ManageDomains?

Otherwise, AuthSub won't work.

Sebastian

Joe Cascio, Jr.

unread,
Mar 26, 2008, 7:21:22 PM3/26/08
to google-co...@googlegroups.com
Yes, I got this doped out yesterday. The problem was that I had mismatched domains for the registered domain and the AuthSub callback domain. I sent this suggestion back on a different thread:

One suggestion: The manage domains page should display the registered domain above the auth sub path text box (labeled: "Target URL path prefix:") with a reminder that these domains must match!  And, part of the "save" button code for that page should be to check that they are the same and reject a mis-matched domain.  That's where I went wrong. It allowed me to put in a call back path that would never have worked!

fox666

unread,
Mar 27, 2008, 9:25:19 PM3/27/08
to Google Contacts API
Tell us

if resolved this problem please. and how you do

tks.


On 26 mar, 20:21, "Joe Cascio, Jr." <joec0...@gmail.com> wrote:
> Yes, I got this doped out yesterday. The problem was that I had mismatched
> domains for the registered domain and the AuthSub callback domain. I sent
> this suggestion back on a different thread:
>
> One suggestion: The manage domains page should display the registered domain
> above the auth sub path text box (labeled: "Target URL path prefix:") with a
> reminder that these domains must match!  And, part of the "save" button code
> for that page should be to check that they are the same and reject a
> mis-matched domain.  That's where I went wrong. It allowed me to put in a
> call back path that would never have worked!
>
> On Wed, Mar 26, 2008 at 7:14 PM, Sebastian Kanthak <
>
>
>
> contacts.skant...@google.com> wrote:
>
> > Did you verify your domain and agreed to the Tos on
> >https://www.google.com/accounts/ManageDomains?
>
> > Otherwise, AuthSub won't work.
>
> > Sebastian
>
> > On Wed, Mar 26, 2008 at 11:59 AM, Walnut <philcornfi...@googlemail.com>
> > >  Ta- Ocultar texto de la cita -
>
> - Mostrar texto de la cita -

Joe Cascio, Jr.

unread,
Mar 27, 2008, 9:43:37 PM3/27/08
to google-co...@googlegroups.com
On this page: https://www.google.com/accounts/ManageDomains you must register the domain you want to call google from, and which will called back by AuthSub. After adding the domain (for instance mydomain.com), it will appear in the list of links under "Manage Registration." Note that the domain name entered must NOT contain the "http://" prefix or any sub-directories after, just "mydomain.com" or "www.mydomain.com", whatever is right for your site.

Click on the domain name you registered and you will be taken to the "Manage Your Domains" page. In the text box " Target URL path prefix", you enter a complete URL with http:// prefix, the exact domain you registered (for instance, mydomain.com and whatever sub-directories you've created to handle the callback. So a typical entry might be http://mydomain.com/authSub.

The important thing is that the domain part of the callback URL must match the registered domain, or it will never work. Unfortunately Google does not emit an error message if there is a mismatch. That is where I ran into trouble.

Good luck!!

JoeC

Sebastian Kanthak

unread,
Mar 27, 2008, 10:50:39 PM3/27/08
to google-co...@googlegroups.com
Joe,

Thanks for explaining and sharing your experience with other users!

On Thu, Mar 27, 2008 at 6:43 PM, Joe Cascio, Jr. <joec...@gmail.com> wrote:
> On this page: https://www.google.com/accounts/ManageDomains you must
> register the domain you want to call google from, and which will called back
> by AuthSub. After adding the domain (for instance mydomain.com), it will
> appear in the list of links under "Manage Registration." Note that the
> domain name entered must NOT contain the "http://" prefix or any
> sub-directories after, just "mydomain.com" or "www.mydomain.com", whatever
> is right for your site.
>
> Click on the domain name you registered and you will be taken to the "Manage
> Your Domains" page. In the text box " Target URL path prefix", you enter a
> complete URL with http:// prefix, the exact domain you registered (for
> instance, mydomain.com and whatever sub-directories you've created to handle
> the callback. So a typical entry might be http://mydomain.com/authSub.
>
> The important thing is that the domain part of the callback URL must match
> the registered domain, or it will never work. Unfortunately Google does not
> emit an error message if there is a mismatch. That is where I ran into
> trouble.

We're looking into improving our UI here, thanks for the suggestion!

fox666

unread,
Mar 28, 2008, 8:56:38 AM3/28/08
to Google Contacts API
Tks, for explaing, but what happens when my account is Gmail or other
account?




On 27 mar, 23:50, Sebastian Kanthak <contacts.skant...@google.com>
wrote:
> Joe,
>
> Thanks for explaining and sharing your experience with other users!
>
>
>
>
>
> On Thu, Mar 27, 2008 at 6:43 PM, Joe Cascio, Jr. <joec0...@gmail.com> wrote:
> > On this page:https://www.google.com/accounts/ManageDomainsyou must
> > register the domain you want to call google from, and which will called back
> > by AuthSub. After adding the domain (for instance mydomain.com), it will
> > appear in the list of links under "Manage Registration." Note that the
> > domain name entered must NOT contain the "http://" prefix or any
> > sub-directories after, just "mydomain.com" or "www.mydomain.com", whatever
> > is right for your site.
>
> > Click on the domain name you registered and you will be taken to the "Manage
> > Your Domains" page. In the text box " Target URL path prefix", you enter a
> > complete URL with http:// prefix, the exact domain you registered (for
> > instance, mydomain.com and whatever sub-directories you've created to handle
> > the callback. So a typical entry might behttp://mydomain.com/authSub.
>
> > The important thing is that the domain part of the callback URL must match
> > the registered domain, or it will never work. Unfortunately Google does not
> > emit an error message if there is a mismatch. That is where I ran into
> > trouble.
>
> We're looking into improving our UI here, thanks for the suggestion!
>
>
>
>
>
> > Good luck!!
>
> > JoeC
>
> > > > - Mostrar texto de la cita -- Ocultar texto de la cita -
>
> - Mostrar texto de la cita -- Ocultar texto de la cita -

Joe Cascio, Jr.

unread,
Mar 28, 2008, 9:10:26 AM3/28/08
to google-co...@googlegroups.com
Sorry, don't understand the question.

fox666

unread,
Mar 28, 2008, 9:29:18 AM3/28/08
to Google Contacts API
Yeap, My case is: I have registred a Domain "xx.com" ok. but the
account to access is for example pe...@gmail.com.
now i don`t now if i have to work on server machine why iam working on
my local pc.

I dont' now to do..



On 28 mar, 10:10, "Joe Cascio, Jr." <joec0...@gmail.com> wrote:

Joe Cascio, Jr.

unread,
Mar 28, 2008, 9:35:18 AM3/28/08
to google-co...@googlegroups.com
Where is the web application running? on xx.com? Or on your local PC?

fox666

unread,
Mar 28, 2008, 9:49:22 AM3/28/08
to Google Contacts API
yeap, its runing(return a token ), my problem is when I try to access
Contacts, now have all code in the server and same error.

fox666

unread,
Mar 28, 2008, 9:58:10 AM3/28/08
to Google Contacts API
look my code by to acces to contacts.



<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Contactos.aspx.cs" Inherits="Contactos" Trace="true"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<%@ Import Namespace="Google.GData.Client" %>
<%@ Import Namespace="Google.GData.Extensions" %>
<%@ Import Namespace="Google.GData.Calendar" %>
<%@ Import Namespace="System.Net" %>

<script runat="server">

void PrintContact() {

Trace.Write("PrintContact", "Token=" + (String)
Session["token"]);
GAuthSubRequestFactory authFactory = new
GAuthSubRequestFactory("cp", "Tigabytes");
authFactory.Token = (String) Session["token"];

// Service ServiceContact = new
Service(authFactory.ApplicationName);
Service ServiceContact = new Service("cp", "prueba-
contactTest-1");
//ServiceContact.setUserCredentials("ed...@tigabytes.com",
"edwin1148");
ServiceContact.SetAuthenticationToken(authFactory.Token);
// ServiceContact.RequestFactory = authFactory;
Google.GData.Client.FeedQuery query= new FeedQuery();

//query.Uri = new Uri("http://www.google.com/m8/feeds/contacts/
tga...@edu.tigabytes.com/base");
query.Uri = new Uri("http://www.google.com/m8/feeds/contacts/
edwin.mondaca%40gmail.com/base");

try
{
// EventFeed calFeed = servicecontact.Query(query);
AtomFeed calFeed = ServiceContact.Query(query);
if (calFeed.Entries.Count > 0)
{

}

AtomEntry retrievedEntry = calFeed.Entries[0];

foreach (Google.GData.Client.AtomFeed entry in
calFeed.Entries)
{
Response.Write("Event: " + "<br/>");
}
}
catch (GDataRequestException gdre)
{
HttpWebResponse response = (HttpWebResponse)gdre.Response;

//bad auth token, clear session and refresh the page
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Trace.Write("Printcontact","Error en codigo");
//Session.Clear();
//Response.Redirect(Request.Url.AbsolutePath, true);
}
else
{
Response.Write("Error processing request: " +
gdre.ToString());
}
}
}

</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Test Site</title>
</head>
<body>

<form id="form1" runat="server">
<h1>First Sample Apis'Contact Fuck</h1>
<div>
<%
GotoAuthSubLink.Visible = false;

Trace.Write("main", "Token" + (String)
Request.QueryString["token"]);
if (Request.QueryString["token"] != null)
{
Trace.Write("main", "Token" + (String)
Request.QueryString["token"]);

Session["token"] = Request.QueryString["token"].ToString();
PrintContact();

}
else if (Request.QueryString["token"] != null)
{

String token = Request.QueryString["token"];
// Session["token"] =
AuthSubUtil.exchangeForSessionToken(token, null).ToString();
Session["token"] = token.ToString();
Response.Write("Request.Url.AbsolutePath :" +
Request.Url.AbsolutePath + ":");
//Response.Redirect(Request.Url.AbsolutePath, true);
}
else //no auth data, print link
{
GotoAuthSubLink.Text = "Login to your Google Account";
GotoAuthSubLink.Visible = true;
//GotoAuthSubLink.NavigateUrl =
AuthSubUtil.getRequestUrl(Request.Url.ToString(),"http://
www.google.com/m8/contacts/feeds/",false,true);
GotoAuthSubLink.NavigateUrl = this.txturl.Text;
}

%>
<table>
<tr>
<td style="width: 100px">
</td>
<td style="width: 773px">
<asp:TextBox ID="txturl" runat="server"
Width="1000px">https://www.google.com/accounts/AuthSubRequest?
scope=http%3A%2F%2Fwww.google.com%2Fm8%2Ffeeds
%2F&session=1&secure=1&next=http://www.tigabytes.com/authsub/
contactos.aspx</asp:TextBox></td>

<td style="width: 353px">%2Fauthsub%2Fcontactos.aspx
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 773px">
</td>
<td style="width: 353px">
</td>
</tr>
<tr>
<td style="width: 100px">
</td>
<td style="width: 773px">
</td>
<td style="width: 353px">
</td>
</tr>
</table>
<asp:HyperLink ID="GotoAuthSubLink" runat="server"/>


</div>
</form>
</body>
</html>

Joe Cascio, Jr.

unread,
Mar 28, 2008, 10:08:57 AM3/28/08
to google-co...@googlegroups.com
I see you have "secure=1" in what looks like the call to get the single-use token.
Do you have a certificate set up and all that? I haven't done secure GETs.

JoeC

fox666

unread,
Mar 28, 2008, 10:16:46 AM3/28/08
to Google Contacts API
Yeap, i just changed it to test, but it does not work. actually it
shows me the same error.




On 28 mar, 11:08, "Joe Cascio, Jr." <joec0...@gmail.com> wrote:
> I see you have "secure=1" in what looks like the call to get the single-use
> token.
> Do you have a certificate set up and all that? I haven't done secure GETs.
>
> JoeC
>
> > tgad...@edu.tigabytes.com/base");
> > %2F&session=1&secure=1&next=http://www.tigabytes.com/authsub/<https://www.google.com/accounts/AuthSubRequest?scope=http%3A%2F%2Fwww...>

Joe Cascio, Jr.

unread,
Mar 28, 2008, 10:26:19 AM3/28/08
to google-co...@googlegroups.com
Ok, I think I see what the problem is. I think you are trying to use a single-use token in the GET contacts, right?
However, your original request to get this token has "session=1" set, which means you are getting back a token that can be use to get a session token.  Try changing this to "session=0" and "secure=0"

JoeC

fox666

unread,
Mar 28, 2008, 10:50:22 AM3/28/08
to Google Contacts API
it gives me the same error, the token, but it fails when i try to send
the request to obtain the contacts

sorry
> > > > </html>- Ocultar texto de la cita -
Reply all
Reply to author
Forward
0 new messages