【问题标题】:Google Cloud Print User credentials required需要 Google 云打印用户凭据
【发布时间】:2017-12-29 23:36:32
【问题描述】:

我正在为我的 (java) Web 应用程序实施谷歌云打印服务,我已按照所有说明进行操作,但仍然收到“需要凭据”作为响应。这是我的代码:

public static void main(String[] args) {
    HttpPost printPost = new HttpPost(
            "https://www.google.com/cloudprint/search?output=json");
    printPost.setHeader("Authorization",
            "GoogleLogin auth=" +getAccessToken());
    printPost.setHeader("scope",
            "https://www.googleapis.com/auth/cloudprint");
    printPost.setHeader("X-CloudPrint-Proxy", "printingTest");
    DefaultHttpClient httpclient = new DefaultHttpClient();
    try {
        HttpResponse printResponse = httpclient.execute(printPost);
        System.out.println(printResponse.getStatusLine());
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public static String getAccessToken() {

    Credential credential = new GoogleCredential.Builder()
            .setTransport(new NetHttpTransport())
            .setJsonFactory(new JacksonFactory())
            .setClientSecrets(
                    "client_id",
                    "client_secret").build();
    try {
        credential
                .setRefreshToken("refresh_token");
        credential.refreshToken();
        return credential.getAccessToken();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;

}

这是一个简单的搜索结果。

【问题讨论】:

    标签: javascript java google-oauth google-cloud-print


    【解决方案1】:

    在设置授权访问令牌时在标题中使用“Bearer”而不是“GoogleLogin auth=”,还要注意带有承载的空格很重要

    【讨论】:

      猜你喜欢
      • 2018-11-08
      • 2014-12-18
      • 2018-11-12
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2012-01-26
      • 2013-06-30
      • 2015-08-14
      相关资源
      最近更新 更多