Google的Oauth2库方法需要一个针对scopes参数的Java列表。将参数编码为List
文字是否合理,如下所示:
public static final List<String> YOUTUBE_SCOPES = Lists.newArrayList(
"https://www.googleapis.com/auth/youtube.upload",
"https://www.googleapis.com/auth/youtube.readonly");
这样我就可以:
SendToGoogleUtils.getGoogleAccountCredential(activity, accountName, YOUTUBE_SCOPES );
Java编译器是否会创建一个有效的YOUTUBE_SCOPES文字?
答案 0 :(得分:5)
ArrayList
不是不可变的,即使它们被声明为final
;这只是意味着变量不能改变,但其内容可以。请参阅: Why can final object be modified?
由于您正在使用Lists.newArrayList
,因此您似乎正在使用Guava。 Guava有一个很好的解决方案:ImmutableList
。所以你的代码是:
public static final List<String> YOUTUBE_SCOPES = ImmutableList.of(
"https://www.googleapis.com/auth/youtube.upload",
"https://www.googleapis.com/auth/youtube.readonly");
那就是说,这里还有另一个问题,那就是如果我想稍后改变这个问题&#34;
我不知道您是否有任何类型的配置文件加载,但请注意,如果您确实需要更改这些范围(例如,如果Google更改了网址),则必须更改链接。如果可以通过重新编译你的罐子来改变它,那么不要担心它;你可能想考虑将这些魔术常量放入某种配置文件中。
答案 1 :(得分:0)
List.of
在现代 Java 中在 Java 9 及更高版本中,我们有 List.of
方法可以轻松生成 unmodifiable List
。
public static final List<String> YOUTUBE_SCOPES =
List.of(
"https://www.googleapis.com/auth/youtube.upload",
"https://www.googleapis.com/auth/youtube.readonly"
)
;