質問編集履歴

2

タイトル等

2020/12/02 17:03

投稿

TKGU
TKGU

スコア3

test CHANGED
@@ -1 +1 @@
1
- PC,MAC & Linux Standalone で書き出したゲーム内で画像付きTweetをしたい
1
+ ゲーム内で画像付きTweetをシェアしたい(PC,MAC & Linux Standalone)
test CHANGED
File without changes

1

コードの追加

2020/12/02 17:03

投稿

TKGU
TKGU

スコア3

test CHANGED
File without changes
test CHANGED
@@ -8,6 +8,10 @@
8
8
 
9
9
  (https://qiita.com/t_hoshiyama/items/35a2966144aeed8ba1ab)
10
10
 
11
+ **・UnityのLet's Tweet in Unityを用いた画像ツイート**
12
+
13
+ (https://teratail.com/questions/59416)
14
+
11
15
 
12
16
 
13
17
  Twitterに上げるためのスクリーンショットは別のシーンで撮っているので、ボタンを押したらツイート画面に飛ぶようにしたいです。(アセット内のフォルダに画像を保存しています。)
@@ -16,6 +20,220 @@
16
20
 
17
21
 
18
22
 
23
+ ↓二つ目のリンクにあったものですが、使い方がわかりません
24
+
25
+
26
+
27
+ ```C#
28
+
29
+ // 1回目のレスポンスJSONをオブジェクトに変換するためのクラス
30
+
31
+ class strctImage
32
+
33
+ {
34
+
35
+ public string image_type;
36
+
37
+ public long w;
38
+
39
+ public long h;
40
+
41
+ }
42
+
43
+ class mediaResponse
44
+
45
+ {
46
+
47
+ public long media_id;
48
+
49
+ public string media_id_string;
50
+
51
+ public long size;
52
+
53
+ public long expires_after_secs;
54
+
55
+ public strctImage image;
56
+
57
+ }
58
+
59
+
60
+
61
+ // Twitter.csに追加するメソッド
62
+
63
+ public static IEnumerator PostTweetWithMedia(string text, string imagePath, string consumerKey, string consumerSecret, AccessTokenResponse response, PostTweetCallback callback)
64
+
65
+ {
66
+
67
+ if (string.IsNullOrEmpty(text) || text.Length > 140)
68
+
69
+ {
70
+
71
+ Debug.Log(string.Format("PostTweet - text[{0}] is empty or too long.", text));
72
+
73
+
74
+
75
+ callback(false);
76
+
77
+ }
78
+
79
+ else
80
+
81
+ {
82
+
83
+ FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
84
+
85
+ BinaryReader bin = new BinaryReader(fileStream);
86
+
87
+ byte[] bytes = bin.ReadBytes((int)bin.BaseStream.Length);
88
+
89
+ var bs64 = Convert.ToBase64String(bytes);
90
+
91
+ bin.Close();
92
+
93
+
94
+
95
+ Dictionary<string, string> mediaParameters = new Dictionary<string, string>();
96
+
97
+ mediaParameters.Add("media_data", bs64);
98
+
99
+ WWWForm mediaForm = new WWWForm();
100
+
101
+ mediaForm.AddField("media_data", bs64);
102
+
103
+ var mediaHeaders = new Dictionary<string, string>();
104
+
105
+ mediaHeaders.Add("Authorization", GetHeaderWithAccessToken("POST", UploadMediaURL, consumerKey, consumerSecret, response, mediaParameters));
106
+
107
+ WWW mediaWeb = new WWW(UploadMediaURL, mediaForm.data, mediaHeaders);
108
+
109
+
110
+
111
+ yield return mediaWeb;
112
+
113
+
114
+
115
+ string media_id_string = "";
116
+
117
+ if (!string.IsNullOrEmpty(mediaWeb.error))
118
+
119
+ {
120
+
121
+ Debug.Log(string.Format("PostMedia - failed. {0}\n{1}", mediaWeb.error, mediaWeb.text));
122
+
123
+ callback(false);
124
+
125
+ yield break;
126
+
127
+ }
128
+
129
+ else
130
+
131
+ {
132
+
133
+ string error = Regex.Match(mediaWeb.text, @"<error>([^&]+)</error>").Groups[1].Value;
134
+
135
+
136
+
137
+ if (!string.IsNullOrEmpty(error))
138
+
139
+ {
140
+
141
+ Debug.Log(string.Format("PostTweet - failed. {0}", error));
142
+
143
+ callback(false);
144
+
145
+ yield break;
146
+
147
+ }
148
+
149
+ else
150
+
151
+ {
152
+
153
+ var res = JsonUtility.FromJson<mediaResponse>(mediaWeb.text);
154
+
155
+ media_id_string = res.media_id_string;
156
+
157
+ }
158
+
159
+ }
160
+
161
+
162
+
163
+ Dictionary<string, string> parameters = new Dictionary<string, string>();
164
+
165
+ parameters.Add("status", text);
166
+
167
+ parameters.Add("media_ids", media_id_string);
168
+
169
+ WWWForm form = new WWWForm();
170
+
171
+ form.AddField("status", text);
172
+
173
+ form.AddField("media_ids", media_id_string);
174
+
175
+
176
+
177
+ // HTTP header
178
+
179
+ var headers = new Dictionary<string, string>();
180
+
181
+ headers.Add("Authorization", GetHeaderWithAccessToken("POST", PostTweetURL, consumerKey, consumerSecret, response, parameters));
182
+
183
+
184
+
185
+ WWW web = new WWW(PostTweetURL, form.data, headers);
186
+
187
+ yield return web;
188
+
189
+
190
+
191
+ if (!string.IsNullOrEmpty(web.error))
192
+
193
+ {
194
+
195
+ Debug.Log(string.Format("PostTweet - failed. {0}\n{1}", web.error, web.text));
196
+
197
+ callback(false);
198
+
199
+ }
200
+
201
+ else
202
+
203
+ {
204
+
205
+ string error = Regex.Match(web.text, @"<error>([^&]+)</error>").Groups[1].Value;
206
+
207
+
208
+
209
+ if (!string.IsNullOrEmpty(error))
210
+
211
+ {
212
+
213
+ Debug.Log(string.Format("PostTweet - failed. {0}", error));
214
+
215
+ callback(false);
216
+
217
+ }
218
+
219
+ else
220
+
221
+ {
222
+
223
+ callback(true);
224
+
225
+ }
226
+
227
+ }
228
+
229
+ }
230
+
231
+ }
232
+
233
+ ```
234
+
235
+
236
+
19
237
  環境
20
238
 
21
239
  ・Unityバージョン_2019.2.12f1