public RefreshTokenResult RefreshAccessToken() { try { var systemConfigDict = _sysContractor.GetSystemInfoDict(new string[] { "DRIVE_TOKEN_SVC", "DRIVE_CLIENT_ID", "DRIVE_CLIENT_SECRET", "DRIVE_REFRESH_TOKEN" }); var refreshSvc = systemConfigDict["DRIVE_TOKEN_SVC"]; var refreshToken = systemConfigDict["DRIVE_REFRESH_TOKEN"]; NameValueCollection outgoingQueryString = System.Web.HttpUtility.ParseQueryString(String.Empty); outgoingQueryString.Add("client_id", systemConfigDict["DRIVE_CLIENT_ID"]); outgoingQueryString.Add("client_secret", systemConfigDict["DRIVE_CLIENT_SECRET"]); outgoingQueryString.Add("refresh_token", systemConfigDict["DRIVE_REFRESH_TOKEN"]); outgoingQueryString.Add("grant_type", "refresh_token"); string postdata = outgoingQueryString.ToString(); var reqTokenRes = _sysContractor.DoSendDataToServer("POST", refreshSvc, postdata, "", "application/x-www-form-urlencoded"); _sysContractor.LogStatus("refresh-token", "log", "", postdata, reqTokenRes.Message, reqTokenRes.Success.ToString()); if (!reqTokenRes.Success) { return(new RefreshTokenResult { Success = false, Message = "Error on refreshing google drive's token.>>" + reqTokenRes.Message }); } var serializer = new JavaScriptSerializer(); var refreshTokenRes = serializer.Deserialize <GoogleDriveRefreshTokenResultModel>(reqTokenRes.Message); var googleDriveToken = string.Format("{0} {1}", refreshTokenRes.token_type, refreshTokenRes.access_token); _sysContractor.LogStatus("refresh-token", "log", "", postdata, "", "New token: " + googleDriveToken); // Update access token. var tokenCon = _context.SystemInfos.Where(s => s.Name == "DRIVE_ACCESS_TOKEN").FirstOrDefault(); if (tokenCon != null) { tokenCon.Value = googleDriveToken; _context.SubmitChanges(); } return(new RefreshTokenResult { Success = true, AccessToken = googleDriveToken }); } catch (Exception ex) { return(new RefreshTokenResult { Success = false, Message = ex.Message }); } }
public ServiceResult ReplyImageContent(int profileId, string contenId, string replyToken) { long id = 0; if (!long.TryParse(contenId, out id)) { return(new ServiceResult { Success = false, Message = "Not a content id." }); } var content = (from a in _context.ContentAccesses join c in _context.Contents on a.ContentId equals c.Id where !a.Inactive && !c.Inactive && c.Id == id && a.ProfileId == profileId select c).FirstOrDefault(); if (content == null) { return(new ServiceResult { Success = false, Message = "Content not found." }); } string imageUrl = content.DriveDownloadUrl; string previewUrl = content.DrivePreviewUrl; List <LineSendMessageModel> messages = new List <LineSendMessageModel>(); messages.Add(new LineSendMessageModel { type = "image", originalContentUrl = imageUrl, previewImageUrl = previewUrl, }); _sysContractor.LogStatus("reply-image", "debug", "", imageUrl, "", "preview:" + previewUrl); return(_lineContractor.ReplyToUser(replyToken, messages)); }