当前位置: 首页 > 知识库问答 >
问题:

当Google Drive API发送推送通知时,HTTP 302

甘君之
2023-03-14

我刚刚设法调用了Google Drive API,以启用文件的推送通知。

设置推送通知的代码如下所示:

public class SampleServlet extends AbstractAppEngineAuthorizationCodeServlet {
    private final static Logger logger = Logger.getLogger(SampleServlet.class.getName());
    private static final long serialVersionUID = 1L;
    // Constants omitted

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws IOException, ServletException {

        AuthorizationCodeFlow authFlow = initializeFlow();
        Credential credential = authFlow.loadCredential(getUserId(req));

        StringBuilder resultFromWatch = new StringBuilder();
        Drive drive = new Drive.Builder(Utils.HTTP_TRANSPORT, Utils.JSON_FACTORY, credential).setApplicationName("t").build();

        try {
            Optional<Channel> channel = watchFile(drive, FILE_ID, CHANNEL_ID, "web_hook", "https://mydomain.appspot.com/drive");
            String channelStringTmp;
            if (channel.isPresent()) {
                channelStringTmp = channel.get().toString();
            } else {
                channelStringTmp = "null...";
            }
            resultFromWatch.append(channelStringTmp);
        } catch (Exception e) {
            resultFromWatch.append(e.getMessage());
        }

        final UserService userService = UserServiceFactory.getUserService();
        final String thisUrl = req.getRequestURI();
        // Send the results as the response
        PrintWriter respWriter = resp.getWriter();
        resp.setStatus(200);
        resp.setContentType("text/html");

        addLoginLogoutButtons(req, resp, resultFromWatch, userService, thisUrl, respWriter);
    }

    private static Optional<Channel> watchFile(Drive service, String fileId,
                                               String channelId, String channelType, String channelAddress) throws IOException {
        final Channel returnValue;
        Channel channel = new Channel();
        channel.setId(channelId);
        channel.setType(channelType);
        channel.setAddress(channelAddress);
        Drive.Files tmp = service.files();
        returnValue = tmp.watch(fileId, channel).execute();
        return Optional.fromNullable(returnValue);
    }

    @Override
    protected AuthorizationCodeFlow initializeFlow() throws ServletException, IOException {
        return Utils.initializeFlow();
    }

    @Override
    protected String getRedirectUri(HttpServletRequest req) throws ServletException, IOException {
        return Utils.getRedirectUri(req);
    }
}

在从浏览器调用servlets doGet并登录之后,我得到以下响应:

{
  "expiration": "1484565747000",
  "id": SAME_ID_AS_DEFINED_IN_SERVLET,
  "kind": "api#channel",
  "resourceId": A_NEW_ID,
  "resourceUri": "https:\/\/www.googleapis.com\/drive\/v3\/files\/FILE_ID?acknowledgeAbuse=false&alt=json"
}
@RestController
@RequestMapping("/drive")
public class ConcreteFileWatchController implements FileWatchController {
    private final static Logger logger = Logger.getLogger(ConcreteFileWatchController.class.getName());

    @RequestMapping(method = RequestMethod.POST)
    @ResponseStatus(value = HttpStatus.OK)
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        logger.info("Received watch call");
    }
}
11:34:35.957
POST
302
0 B
22 ms
APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)
/drive
 10.72.94.97 - - [16/Jan/2017:11:34:35 +0100] "POST /drive HTTP/1.1" 302 - - "APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)" "mydomain.appspot.com" ms=22 cpu_ms=0 cpm_usd=0 loading_request=0 instance=- app_engine_release=1.9.48 trace_id=d0e888dd3989e353344e40e41758fdf4
{
  protoPayload: {
    @
  type: "type.googleapis.com/google.appengine.logging.v1.RequestLog"
  appId: "p~blabla"
  versionId: "201t113050"
  requestId: "587ca1bb00ff05706f727465726261636b656e640001323031373031313674313133303530000100"
  ip: "10.76.94.97"
  startTime: "2017-01-16T10:34:35.957904Z"
  endTime: "2017-01-16T10:34:35.980366Z"
  latency: "0.022462s"
  method: "POST"
  resource: "/drive"
  httpVersion: "HTTP/1.1"
  status: 302
  userAgent: "APIs-Google; (+https://developers.google.com/webmasters/APIs-Google.html)"
  host: "blabla.appspot.com"
  instanceIndex: -1
  finished: true
  appEngineRelease: "1.9.48"
  traceId: "d0e888dd390f41758fdf4"
  first: true
}
insertId: "587cf6df9ded23f7"
httpRequest: {
status: 302
}
resource: {
type: "gae_app"
labels: {…}
}
timestamp: "2017-01-16T10:34:35.957904Z"
labels: {
appengine.googleapis.com/version_id: "2017013050"
clone_id: ""
appengine.googleapis.com/clone_id: ""
appengine.googleapis.com/module_id: "default"
version_id: "20170116t113050"
request_id: "587ca1bb00ff0e9dd0f39f31350001707e6561737974696d657265706f721373031313674313133303530000100"
appengine.googleapis.com/request_id: "587ca1bb00ff0e9dd0f39f31350001707e6561737974696d6572653674313133303530000100"
module_id: "default"
}
logName: "projects/blabla/logs/appengine.googleapis.com%2Frequest_log"
operation: {
id: "587ca1bb00ff0e9dde640001323031373031313674313133303530000100"
producer: "appengine.googleapis.com/request_id"
first: true
last: true
}
}

web.xml:

<web-app>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
    </context-param>
    <filter>
        <filter-name>CORS</filter-name>
        <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CORS</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value></param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet>
        <servlet-name>PlusBasicServlet</servlet-name>
        <servlet-class>packagename.PlusBasicServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>PlusBasicServlet</servlet-name>
        <url-pattern>/plusbasicservlet</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>PlusSampleServlet</servlet-name>
        <servlet-class>packagename.PlusSampleServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>PlusSampleServlet</servlet-name>
        <url-pattern>/plussampleservlet</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>FileWatchTestServlet</servlet-name>
        <servlet-class>packagename.ConcreteFileWatchController</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>FileWatchTestServlet</servlet-name>
        <url-pattern>/drive</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>PlusSampleAuthCallbackServlet</servlet-name>
        <servlet-class>packagename.PlusSampleAuthCallbackServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>PlusSampleAuthCallbackServlet</servlet-name>
        <url-pattern>/oauth2callback</url-pattern>
    </servlet-mapping>
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>any</web-resource-name>
            <url-pattern>/plussampleservlet</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>*</role-name>
        </auth-constraint>
    </security-constraint>
</web-app>

日志截图:

共有1个答案

杜元明
2023-03-14

由于您的回调是通过Spring MVC提供的,请确保您没有在其他地方配置任何隐藏的重定向。您应该首先使用常规servlet测试回调:

public class FileWatchTestServlet extends HttpServlet {
    private final static Logger logger = Logger.getLogger(FileWatchTestServlet.class.getSimpleName());

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        logger.info("Received watch call");
    }
}

在web.xml中:

<servlet>
    <servlet-name>FileWatchTestServlet</servlet-name>
    <servlet-class>com.mydomain.FileWatchTestServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>FileWatchTestServlet</servlet-name>
    <url-pattern>/drive</url-pattern>
</servlet-mapping>

如果302消失了,问题就出在Spring MVC的dispatcher上,所以您应该在那里寻找任何可能导致重定向的东西(例如,在您的视图中)。

 类似资料:
  • 这是舱单 这是我的注册令牌类 这是我的Firebase服务类

  • 如何通过Azure从我的UWP-App向不同设备上的应用程序的其他实例发送推送通知? 以下是注册设备以接收推送的说明。(这是可行的)第二部分是关于如何在控制台应用程序上发送推送(这也是可行的)https://azure.microsoft.com/en-us/documentation/articles/notification-hubs-windows-store-dotnet-get-star

  • 我想使用按钮单击中的GCM将推送通知从一个设备发送到多个设备。我遵循了GCM的所有流程。我获得了设备的服务器密钥和注册ID,但没有使用GCM获得推送通知。我也在谷歌上搜索过,但没有找到正确的解决方案。 请建议我如何在多设备上发送推送通知。 MAYActivity.java

  • 我在我的应用程序中使用Google日历API。 我面临的问题是谷歌没有给我发送推送通知。 我在这里设置我的应用程序https://console.developers.google.com/ 已验证域名:https://console.developers.google.com/apis/credentials/domainverification 观看日历:https://developers.

  • 我正在写一个iOS的应用程序,使用laravel的API和谷歌Firebase的推送通知。当我使用Firebase云消息传递发送消息推送时,它会进入我的设备。当我使用laravel发送推送通知时,它不会影响。这里是我的脚本发送推送通知laravel: 它返回一个成功的结果,但通知未发送到iOS设备。 PS:它可以在Android设备上成功运行。

  • 我为沙盒iOS设置了推送通知,它们曾经工作过,但由于某种原因目前不工作。这是来自云观察日志的错误AWS SNS: 这是我的发送代码(AWS Lambda调用此代码): Lambda方法的返回数据: 我已经将问题缩小到不与我的lambda方法关联,因为我试图发布到直接从SNS控制台创建的endpoint。我还想知道为什么这个旧的实现在过去,一个月或两个月前,仍然有效,而现在不再有效。我认为这可能与过