Esempio n. 1
0
        public ServiceResult PushToUser(string lineUserId, List <LineSendMessageModel> messages)
        {
            try
            {
                var    systemConfigDict = _sysContractor.GetSystemInfoDict(new string[] { "LINE_SVC", "LINE_ACCESS_TOKEN" });
                string serviceUrl       = systemConfigDict["LINE_SVC"] + "/bot/message/push";
                string lineAccessToken  = systemConfigDict["LINE_ACCESS_TOKEN"];

                var info = new
                {
                    to       = lineUserId,
                    messages = messages
                };
                var    serializer = new JavaScriptSerializer();
                string parameters = serializer.Serialize(info);

                var res = _sysContractor.DoPostJSON(serviceUrl, parameters, lineAccessToken);
                return(res);
            }
            catch (Exception ex)
            {
                return(new ServiceResult {
                    Success = false, Message = ex.Message
                });
            }
        }
Esempio n. 2
0
 public RequestContractor(DbConnection connection)
     : base(connection)
 {
     _driveContractor = new GoogleContractor(connection);
     _lineContractor  = new LineContractor(connection);
     _sysContractor   = new SystemContractor(connection);
     _configs         = _sysContractor.GetSystemInfoDict(new string[] { "DRIVE_IMG_SCHEME", "DRIVE_UPLOAD_SVC", "DRIVE_ACCESS_TOKEN", "DRIVE_REFRESH_TOKEN", "LINE_ACCESS_TOKEN", "LINE_IMG_SCHEME", "LINE_SVC" });
 }
Esempio n. 3
0
        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
                });
            }
        }