在没有授权页面的情况下以编程方式将照片上传到ASP.NET中的Flickr

时间:2015-08-16 10:12:55

标签: c# asp.net flickr

我试图通过ASP.NET文件上传控件和flickrnet将照片上传到Flickr。但是flickrnet.codeplex.com下的示例是,将页面重定向到flickr进行授权并返回。

有没有办法上传文件而没有重定向到asp.net C#中的授权页面?

1 个答案:

答案 0 :(得分:0)

是。我能够。首先,您需要FlickrManager实例才能使用此代码

 public class FlickrManager
{
    public const string ApiKey = "get it from flickr website";
    public const string SharedSecret = "get it from flickr website";

    public static Flickr GetInstance()
    {
        return new Flickr(ApiKey, SharedSecret);
    }

    public static Flickr GetAuthInstance()
    {
        var f = new Flickr(ApiKey, SharedSecret);
        if (OAuthToken != null)
        {
            f.OAuthAccessToken = OAuthToken.Token;
            f.OAuthAccessTokenSecret = OAuthToken.TokenSecret;
        }
        return f;
    }

    public static OAuthAccessToken OAuthToken
    {
        get
        {
            if (HttpContext.Current.Request.Cookies["OAuthToken"] == null)
            {
                return null;
            }
            var values = HttpContext.Current.Request.Cookies["OAuthToken"].Values;
            return new OAuthAccessToken
                       {
                           FullName = values["FullName"],
                           Token = values["Token"],
                           TokenSecret = values["TokenSecret"],
                           UserId = values["UserId"],
                           Username = values["Username"]
                       };
        }
        set
        {
            // Stores the authentication token in a cookie which will expire in 1 hour
            var cookie = new HttpCookie("OAuthToken")
            {
                Expires = DateTime.UtcNow.AddHours(1),
            };
            cookie.Values["FullName"] = value.FullName;
            cookie.Values["Token"] = value.Token;
            cookie.Values["TokenSecret"] = value.TokenSecret;
            cookie.Values["UserId"] = value.UserId;
            cookie.Values["Username"] = value.Username;
            HttpContext.Current.Response.AppendCookie(cookie);
        }
    }
}

创建上面的类之后,您可以在aspx页面的任何位置开始使用以下方法。如果您有挑战,请告诉我。

 public string FilckerUpload(string url, string title, string description, string tags)
    {
        OAuthAccessToken accessToken = new OAuthAccessToken();
        accessToken.FullName = "your app name";
        accessToken.Token = "get it from Flickr Website for your login";
        accessToken.TokenSecret = "get it from Flickr Website for your login";
        accessToken.UserId = "get it from Flickr Website for your login";
        accessToken.Username = "get it from Flickr Website for your login";
        FlickrManager.OAuthToken = accessToken;
        Flickr flickr = FlickrManager.GetAuthInstance();
        string FileuploadedID = flickr.UploadPicture(@url, title, description, tags, true, false, false);
        PhotoInfo oPhotoInfo = flickr.PhotosGetInfo(FileuploadedID);
        return oPhotoInfo.Small320Url;
    }