I'm trying upload photo to picasa i use this C# code
I'm successfully get access token , but when i use it with
authFactory.Token = googleRespnse.access_token;
PicasaService service = new PicasaService(authFactory.ApplicationName);
service.RequestFactory = authFactory;
i got error message
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.Script.Serialization; using Google.GData.Client; using Google.Picasa; using Google.GData.Photos; using System.Net; using System.IO;
public partial class _2gplus_move_auth_goauth : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string GKey = System.Configuration.ConfigurationManager.AppSettings["GKey"]; string GSecret = System.Configuration.ConfigurationManager.AppSettings["GSecret"]; string scop = "http://picasaweb.google.com/data/feed/api/user/default/"; string next = System.Configuration.ConfigurationManager.AppSettings["GMyWebSite"] + System.Configuration.ConfigurationManager.AppSettings["GPostAuthorizeRedirectURL"];
if ((Request["token"] == null) && (Request["error"] == null) && (Request["fromfb"]=="ok"))
{
string Url = string.Format("https://accounts.google.com/o/oauth2/auth?client_id={0}&redirect_uri={1}&scope={2}&response_type=code", GKey, next, scop);
Response.Redirect(Url);
}
else if((Page.Request.QueryString["goauth"] != null) && (Page.Request.QueryString["goauth"] == "ok")) //if (Request["goauth"] == "ok")
{
if ((Page.Request.QueryString["code"] != null) && (Page.Request.QueryString["code"] != ""))
{
string code = Page.Request.QueryString["code"];
string link = string.Format("https://accounts.google.com/o/oauth2/token");
string parm = string.Format("client_id={0}&client_secret={1}&redirect_uri={2}&code={3}&grant_type=authorization_code", GKey, GSecret, next, code);
string response = webFetch(link, parm);
JavaScriptSerializer js = new JavaScriptSerializer();
GooGleRespnse googleRespnse=js.Deserialize<GooGleRespnse>(response);
GAuthSubRequestFactory authFactory = new GAuthSubRequestFactory("lh2","exampleCo-exampleApp-1");
authFactory.Token = googleRespnse.access_token;
PicasaService service = new PicasaService(authFactory.ApplicationName);
service.RequestFactory = authFactory;
string username = "default";
AlbumEntry newEntry = new AlbumEntry();
newEntry.Title.Text = "New album";
newEntry.Summary.Text = "This is an album";
AlbumAccessor ac = new AlbumAccessor(newEntry);
ac.Access = "public";
Uri feedUri = new Uri(PicasaQuery.CreatePicasaUri(username));
PicasaEntry createdEntry = (PicasaEntry)service.Insert(feedUri, newEntry);
}
else
{
Server.Transfer("gonotoauth.aspx");
}
Session["google_access_token"] = Request["access_token"];
}
}
private string webFetch(string link, string parameter)
{
string responseFromServer = "";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
request.Method = "POST";
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(parameter);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (HttpStatusCode.OK == response.StatusCode)
{
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
responseFromServer = reader.ReadToEnd();
response.Close();
}
return responseFromServer;
}
class GooGleRespnse
{
public string access_token {get;set;}
public string expires_in { get; set; }
public string refresh_token { get; set; }
}
}
- goauth.cs 4.11KB
Comment #1
Posted on Jul 21, 2011 by Helpful GiraffeI might not be understanding your question, but I don't see how this GWT OAuth 2.0 library has anything to do with failed access in a C# app?
Also, it looks like you're using the access token to get data from the Picasa GData API, which doesn't support OAuth 2.0, just OAuth 1.0a.
Status: Invalid
Labels:
Type-Defect
Priority-Medium