###前提・実現したいこと
C#ストアアプリ用.NET Frameworkを用い(Windowsストアアプリ)、GmailやYahoo!メールなどでログインし
マルチアカウントでメールの送受信が可能なメーラーソフトを作成しています。
メールの送信(SMTP)にOAuth2.0での認証を考えているのですが、
OAuthに対応したSMTP送信の方法がなかなか見つからず困っています。
下記の通りGoogleでのアクセストークン、リフレッシュトークンの取得には成功しています。
取得したアクセストークンを用い、メールを送信する方法、ライブラリなどございましたらご教授のほどお願いいたします。
###該当のソースコード
/**
* リクエストトークンの取得
*/
private async void GetRequestToken()
{
string res;
var consumerKey = [CONSUMER KEY]; //認証に使うURL var callbackURL = [CALLBACK URL]; var requestTokenURL = "https://accounts.google.com/o/oauth2/auth"; var authorizeURL = "https://www.googleapis.com/auth/userinfo.profile email"; //パラメータをつくる string param = requestTokenURL + "?client_id=" + consumerKey + "&redirect_uri=" + callbackURL + "&scope=" + authorizeURL + "&approval_prompt=force" + "&response_type=code" + "&access_type=offline"; //認証ページにリダイレクト WebAuthenticationResult result = await WebAuthenticationBroker.AuthenticateAsync( WebAuthenticationOptions.None, new Uri(param), new Uri(callbackURL) ); //認証レスポンスを取得 if (result.ResponseStatus == WebAuthenticationStatus.Success) { res = result.ResponseData.ToString(); res = res.Substring(res.IndexOf("code=")+5); //アクセストークンの取得に進む GetAccessToken(res); } } /** * アクセストークンの取得 */ private async void GetAccessToken(string code) { var consumerKey = [CONSUMER KEY]; var consumerKeySecret = [CONSUMER SECRET]; var callbackURL = [CALLBACK URL]; var accessTokenURL = "https://accounts.google.com/o/oauth2/token"; var authorizeURL = "https://www.googleapis.com/auth/userinfo.profile email"; //Dictionaryオブジェクト Dictionary<string, string> param = new Dictionary<string, string>(); param["code"] = code; param["grant_type"] = "authorization_code"; param["redirect_uri"] = callbackURL; param["client_id"] = consumerKey; param["client_secret"] = consumerKeySecret; HttpClient httpClient = new HttpClient(); httpClient.MaxResponseContentBufferSize = int.MaxValue; HttpContent content = new FormUrlEncodedContent(param); var response = await httpClient.PostAsync(accessTokenURL, content);
//アクセストークン、リフレッシュトークンが含まれるデータが返る
string result = await response.Content.ReadAsStringAsync();
//テキスト成形 int A_first = (result.IndexOf("\"access_token\" : \"") + 18); int A_charcnt = result.IndexOf("\",\n \"expires_in\"") - A_first; int R_first = (result.IndexOf("\"refresh_token\" : \"") + 19); int R_chercnt = result.IndexOf("\",\n \"token_type\"") - R_first; AccessToken = result.Substring(A_first, A_charcnt); RefrashToken = result.Substring(R_first, R_chercnt); }

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。