简体   繁体   中英

401 unauthorized using google task

I'm learning to use google task api rest.

i have succeed getting access tokken.

now i am trying to get tasklist using this rest url: https://www.googleapis.com/tasks/v1/users/@me/lists

In google task documentation write that i need to sent Access tokens by HTTP Authorization header but i don't know how link

i have search for result in the web but didn't found a solution. I have also searched this site for solution, but didn't get one.

i am getting 401 unauthorized error in when trying to request.GetResponse(); this is my code

 private AccessToken _accessToken = null;
private string Apikey = "my api key";
protected void Page_Load(object sender, EventArgs e)
{
    _accessToken = (AccessToken)Session["AccessTokken"];
    string _customerkey = "my customer key";
    string _customerSecret = "my customer secret key";
    Response.Write(_accessToken.Token);

    string nostring = "";
    string nnString = "";
    OAuthBase oauth = new OAuthBase();
    Uri t = new Uri("https://www.googleapis.com/tasks/v1/users/@me/lists");
    string u = oauth.GenerateSignature(t, _customerkey, _customerSecret, _accessToken.Token,
                                       _accessToken.TokenSecret, "GET", oauth.GenerateTimeStamp(),
                                       oauth.GenerateNonce(), out nostring, out nnString);

    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(nostring);
    request.Method = "GET";

    //request.Headers.Add("Authorization: Bearer " + _accessToken.Token);
    WebResponse response = request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    string responseString = reader.ReadToEnd();
    reader.Close();


    Response.Write(responseString);

There is a Google publishes a .Net library for accessing Google Api, this includes helpers for doing the authentication I would suggest using this Api and starting from the samples the project publishers (including Task Api Sample ).

If you want to carry on hand cranking the Api call you can either just use the communications library , or look at the source code and see how Google has done the communication.

EDIT

From the source linked above you can find the OAuth Authenticator which adds http header System.Net.HttpRequestHeader.Authorization with the value specified in OAuthUtil.GenerateHeader . The relevant section copied below.

StringBuilder sb = new StringBuilder();
sb.Append("Authorization: OAuth oauth_version=\"1.0\",");
sb.AppendFormat("oauth_nonce=\"{0}\",", EncodingPerRFC3986(nonce));
sb.AppendFormat("oauth_timestamp=\"{0}\",", EncodingPerRFC3986(timeStamp));
sb.AppendFormat("oauth_consumer_key=\"{0}\",", EncodingPerRFC3986(consumerKey));
if (!String.IsNullOrEmpty(token))
{
    sb.AppendFormat("oauth_token=\"{0}\",", EncodingPerRFC3986(
}
sb.Append("oauth_signature_method=\"HMAC-SHA1\",");
sb.AppendFormat("oauth_signature=\"{0}\"", EncodingPerRFC3986(signature));

return sb.ToString();

You can use just this bottom layer of the Api and not worry about the generated services. Remember this code is covered by http://www.apache.org/licenses/LICENSE-2.0 so it may be easier in a legal seance to use the DLL for this specific function rather then coping it directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM